0

I wonder how to get user-id list who liked the specified tweet id.

I can see the number of favorites of the tweet by using 'favorite count', but would like to know WHO liked the tweet.

The same question as Twitter API - Getting list of users who favorited a status , which was written in python2, so I re-write in python3. However it seems it does not work any longer.

def get_user_ids_who_likes(post_id):
    try:
        url = 'https://twitter.com/i/activity/favorited_popup?id=' + str(post_id)
        json_data = urllib.request.urlopen(url).read()
        found_ids = re.findall(r'data-user-id=\\"+\d+', json_data.decode('utf-8'))
        unique_ids = list(set([re.findall(r'\d+', match)[0] for match in found_ids]))
        return unique_ids
    except urllib.error.HTTPError:
        return False

Are there any solutions?

Ted
  • 63
  • 7

1 Answers1

3

Use tweepy.Client.get_liking_users().


Client.get_liking_users(id, *, expansions, media_fields, place_fields, poll_fields, tweet_fields, user_fields)

Allows you to get information about a Tweet’s liking users.

Parameters:

  • id (Union[int, str]) – Tweet ID of the Tweet to request liking users of.
  • expansions (Union[List[str], str])expansions
  • media_fields (Union[List[str], str])media_fields
  • place_fields (Union[List[str], str])place_fields
  • poll_fields (Union[List[str], str])poll_fields
  • tweet_fields (Union[List[str], str])tweet_fields
  • user_fields (Union[List[str], str])user_fields

Returns:

Return type Union[dict, requests.Response, Response]

References:

https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/get-tweets-id-liking_users

Ari Cooper-Davis
  • 3,374
  • 3
  • 26
  • 43