通过Tweepy获取Twitter中的所有关注者id

2024-04-28 19:44:35 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有可能得到像麦当劳这样拥有100多万关注者的账户的完整关注者名单?

我使用Tweepy并遵循代码:

c = tweepy.Cursor(api.followers_ids, id = 'McDonalds')
ids = []
for page in c.pages():
     ids.append(page)

我也试试这个:

for id in c.items():
    ids.append(id)

但我总是得到“超过速率限制”的错误,只有5000个跟随者ID。


Tags: 代码inapiididsforpage账户
3条回答

为了避免速率限制,您可以/应该在下一个follower页面请求之前等待。看起来很老套,但很管用:

import time
import tweepy

auth = tweepy.OAuthHandler(..., ...)
auth.set_access_token(..., ...)

api = tweepy.API(auth)

ids = []
for page in tweepy.Cursor(api.followers_ids, screen_name="McDonalds").pages():
    ids.extend(page)
    time.sleep(60)

print len(ids)

希望能有所帮助。

建立连接时使用速率限制参数。api将在速率限制内进行自我控制。

睡眠暂停还不错,我用它来模拟一个人,并在一个时间范围内展开活动,最后以api速率限制为控制。

api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, compression=True)

还可以添加try/except来捕获和控制错误。

示例代码 https://github.com/aspiringguru/twitterDataAnalyse/blob/master/sample_rate_limit_w_cursor.py

为了便于管理,我把钥匙放在一个外部文件中。

https://github.com/aspiringguru/twitterDataAnalyse/blob/master/keys.py

亚历克斯的回答是好的,但是没有人提到这些文件。回答问题的正确信息和解释存在于Twitter API documentation中。从文档中:

Results are given in groups of 5,000 user IDs and multiple “pages” of results can be navigated through using the next_cursor value in subsequent requests.

相关问题 更多 >