【发布时间】:2018-09-10 09:14:03
【问题描述】:
这个https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/get-favorites-list 与 PHP 配合得很好。我想知道如何将它与 tweepy 或任何 python 模块一起使用?
【问题讨论】:
标签: python python-3.x twitter tweepy
这个https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/get-favorites-list 与 PHP 配合得很好。我想知道如何将它与 tweepy 或任何 python 模块一起使用?
【问题讨论】:
标签: python python-3.x twitter tweepy
使用 Tweepy 可以实现您想要做的事情。您可以使用光标搜索用户的所有转推,然后提取您需要的信息。响应的结构与您正在查看的 php api 的响应非常相似。
要使用以下代码 tweepy 需要访问您的开发者帐户应用程序,您可以在其documentation 上找到有关如何执行此操作的基本指南。
搜索代码:
# This variable hold the username or the userid of the user you want to get favorites from
# This needs to be the users unique username
User = "@StackOverflow"
# Cursor is the search method this search query will return 20 of the users latest favourites just like the php api you referenced
for favorite in tweepy.Cursor(api.favorites, id=User).items(20):
# To get diffrent data from the tweet do "favourite" followed by the information you want the response is the same as the api you refrenced too
#Basic information about the user who created the tweet that was favorited
print('\n\n\nTweet Author:')
# Print the screen name of the tweets auther
print('Screen Name: '+str(favorite.user.screen_name.encode("utf-8")))
print('Name: '+str(favorite.user.name.encode("utf-8")))
#Basic information about the tweet that was favorited
print('\nTweet:')
# Print the id of the tweet the user favorited
print('Tweet Id: '+str(favorite.id))
# Print the text of the tweet the user favorited
print('Tweet Text: '+str(favorite.text.encode("utf-8")))
# Encoding in utf-8 is a good practice when using data from twitter that users can submit (it avoids the program crashing because it can not encode characters like emojis)
在您的 OAuth 下实现该代码以获取身份验证代码
【讨论】: