Tweepy:如何获取喜欢(喜欢)指定推文的用户 ID

Tweepy : How to get user id who liked(fav) the specified tweet

我想知道如何获得 user-id 喜欢指定推文 ID 的列表。

我可以使用 'favorite count' 查看推文的收藏数量,但想知道谁喜欢这条推文。

同样的问题 Twitter API - Getting list of users who favorited a status ,写在python2,所以我re-write写在python3。 然而它似乎不再起作用了。

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

有什么解决办法吗?

使用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