简体   繁体   中英

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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