问题
我在用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错误,而我仍然需要保留请求。
代码
这是我抛出错误的代码。
#!/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通过输入交互式提示符,我找到了抛出错误的行。
for follower in tweepy.Cursor(api.followers, me).items():
        print follower它给了我错误
**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}]**发布于 2015-08-27 14:43:35
API().followers方法实际上是GET followers/list,它仅限于每个窗口15个请求(在下一个时代之前)。每个调用都返回20用户列表,该列表由默认列出,如果您达到了极限超出错误,我确信经过身份验证的用户有300多个关注者。
解决方案是增加每次调用期间接收的用户列表的长度,以便在15次调用内能够获取所有用户。按以下方式修改代码
for follower in tweepy.Cursor(api.followers, id = me.id, count = 50).items():
    print followercount表示每个请求要获取的用户数。
count的最大值可以是200,这意味着在15分钟内,您只能获得200x15= 3000跟随者,这在一般情况下就足够了。
https://stackoverflow.com/questions/29332259
复制相似问题