首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >超过tweepy.TweepError利率限制

超过tweepy.TweepError利率限制
EN

Stack Overflow用户
提问于 2015-03-29 17:18:41
回答 1查看 3.7K关注 0票数 4

问题

我在用Python和Tweepy写一个Twitter机器人。昨晚我成功地让机器人开始工作,但是在跟踪了60个人之后,机器人开始抛出一个错误,上面写着[{u'message': u'Rate Limit Exceeded', u'code': 88}]。我知道我只被允许对Twitter进行一定数量的调用,并且我找到了此链接,它显示了我可以在每个函数上调用多少次。在查看了我的代码之后,我发现在我说for follower in tweepy.Cursor(api.followers, me).items():的地方抛出了错误。在我发现的页面上,上面写着我收到了多少个请求,上面写着我每15分钟就会收到15个请求来吸引我的追随者。我已经等了一夜,今天早上我重新尝试了这段代码,但是它仍然抛出了相同的错误。我不明白为什么Tweepy会抛出一个rate limit exceeded错误,而我仍然需要保留请求。

代码

这是我抛出错误的代码。

代码语言:javascript
运行
复制
#!/usr/bin/python

import tweepy, time, pprint

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True)
me = api.me()

pprint.pprint(api.rate_limit_status())

while True:
    try:
        for follower in tweepy.Cursor(api.followers, me).items():
            api.create_friendship(id=follower.id)
        for follower in tweepy.Cursor(api.friends, me).items():
            for friend in tweepy.Cursor(api.friends, follower.id).items():
                if friend.name != me.name:
                    api.create_friendship(id=friend.id)
    except tweepy.TweepError, e:
        print "TweepError raised, ignoring and continuing."
        print e
        continue

通过输入交互式提示符,我找到了抛出错误的行。

代码语言:javascript
运行
复制
for follower in tweepy.Cursor(api.followers, me).items():
        print follower

它给了我错误

代码语言:javascript
运行
复制
**Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    for follower in api.followers(id=me.id):
  File "C:\Users\Lane\AppData\Local\Enthought\Canopy\User\lib\site-packages\tweepy\binder.py", line 239, in _call
    return method.execute()
  File "C:\Users\Lane\AppData\Local\Enthought\Canopy\User\lib\site-packages\tweepy\binder.py", line 223, in execute
    raise TweepError(error_msg, resp)
TweepError: [{u'message': u'Rate limit exceeded', u'code': 88}]**
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-27 14:43:35

API().followers方法实际上是GET followers/list,它仅限于每个窗口15个请求(在下一个时代之前)。每个调用都返回20用户列表,该列表由默认列出,如果您达到了极限超出错误,我确信经过身份验证的用户有300多个关注者。

解决方案是增加每次调用期间接收的用户列表的长度,以便在15次调用内能够获取所有用户。按以下方式修改代码

代码语言:javascript
运行
复制
for follower in tweepy.Cursor(api.followers, id = me.id, count = 50).items():
    print follower

count表示每个请求要获取的用户数。

count的最大值可以是200,这意味着在15分钟内,您只能获得200x15= 3000跟随者,这在一般情况下就足够了。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29332259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档