简体   繁体   English

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

[英]How to get an user's liked (favourited) tweets with tweepy-python3?

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

What you're looking to do can be achieved with Tweepy. 使用Tweepy可以实现您的目标。 You can use cursor to search for all of the retweets from a user, then extract the information you need. 您可以使用光标搜索用户的所有转推,然后提取所需的信息。 The structure of the repsonse is very similar to the response from the php api you are looking at. repsonse的结构非常类似于你正在看的php api的响应。

To use the following code tweepy needs access to your developer accounts app, you can find a basic guide on how to do this on their documentation . 要使用以下代码,需要访问您的开发者帐户应用程序,您可以找到有关如何在其文档中执行此操作的基本指南。

Code For Searching: 搜索代码:

# 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)

Implement that code under your OAuth for authentication code 在您的OAuth下实现该代码以获取身份验证代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM