使用Tweepy获取特定Twitter用户的最新关注者



我目前使用get_users_following(user_id_account, max_results=1000)函数来获取帐户的关注列表,以了解他在twitter上关注的人。到目前为止,只要用户关注的人少于1000人,它就能很好地工作,因为API限制了最多1000个用户的列表。问题是,当他跟踪超过1000个人时,我无法得到最后一个人。该函数总是给我前1000,而忽略最后的

https://docs.tweepy.org/en/stable/client.html tweepy.Client.get_users_followers

https://developer.twitter.com/en/docs/twitter-api/users/follows/api-reference/get-users-id-following

有一个pagination_token参数,但我不知道如何使用它。我想要的只是最后X个新关注的人,这样我就可以将他们添加到数据库中,并为每个新条目获得通知

client = tweepy.Client(api_token)
response = client.get_users_following(id=12345, max_results=1000)

可以直接翻到最后一页吗?

Tweepy使用Paginator类处理分页(参见这里的文档)。

例如,如果您想查看所有的页面,您可以这样做:

# Use the wait_on_rate_limit argument if you don't handle the exception yourself
client = tweepy.Client(api_token, wait_on_rate_limit=True)
# Instantiate a new Paginator with the Tweepy method and its arguments
paginator = tweepy.Paginator(client.get_users_following, 12345, max_results=1000)
for response_page in paginator:
print(response_page)

或者您也可以直接获得用户以下内容的完整列表:

# Instantiate a new Paginator with the Tweepy method and its arguments
paginator = tweepy.Paginator(client.get_users_following, 12345, max_results=1000)
for user in paginator.flatten():  # Without a limit argument, it gets all users
print(user.id)

最新更新