如何用tweepy-python3获得用户喜欢(赞成)的推文?

时间:2018-03-31 15:40:07

标签: python python-3.x twitter tweepy

这个https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/get-favorites-list适用于PHP。我想知道如何在tweepy或任何python模块中使用它?

1 个答案:

答案 0 :(得分:2)

使用Tweepy可以实现您的目标。您可以使用光标搜索用户的所有转推,然后提取所需的信息。 repsonse的结构与你正在看的php api的响应非常相似。

要使用以下代码,需要访问您的开发者帐户应用,您可以在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下实施该代码以获取身份验证代码