简体   繁体   English

tweepy.TweepError超出限速

[英]tweepy.TweepError Rate Limit Exceeded

Question

I was writing a Twitter bot with Python an Tweepy. 我正在用Python写一个Twitter机器人Tweepy。 I successfully got the bot to work last night, however, after following 60 people, the bot started throwing an error that says [{u'message': u'Rate Limit Exceeded', u'code': 88}] . 我昨晚成功地让机器人工作了,然而,在跟随60个人后,机器人开始抛出一个错误,上面写着[{u'message': u'Rate Limit Exceeded', u'code': 88}] I understand that I am only allowed to do a certain amount of calls to the Twitter API and I have found this link that shows how many calls I can make on each of these functions. 我知道我只允许对Twitter API进行一定数量的调用,我发现这个链接显示了我可以对这些函数进行多少次调用。 After reviewing my code, I have found that the error is being thrown where I am saying for follower in tweepy.Cursor(api.followers, me).items(): . 在查看我的代码之后,我发现错误是在我for follower in tweepy.Cursor(api.followers, me).items():说的地方。 On the page that I found that says how many requests I get, it says that I get 15 requests for every 15 minutes for getting my followers. 在我发现的页面上说我收到了多少请求,它表示我每15分钟收到15个请求以获取我的关注者。 I have waited overnight and I retried the code this morning, however, it was still throwing the same error. 我等了一夜,我今天早上重试了代码,然而,它仍然抛出同样的错误。 I don't understand why Tweepy is throwing a rate limit exceeded error whenever I should still have requests left. 我不明白为什么每当我还有请求时,Tweepy会rate limit exceeded错误。

Code

Here is my code that's throwing the error. 这是我的代码抛出错误。

#!/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

I have found the line that throws the error by typing in the interactive prompt 我通过在交互式提示中键入来找到引发错误的行

for follower in tweepy.Cursor(api.followers, me).items():
        print follower

where it gives me the error 它给了我错误的地方

**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}]**

API().followers method is actually GET followers/list , which is limited to 15 requests per window (before next epoch). API().followers方法实际上是GET followers/list ,每个窗口限制为15个请求(在下一个纪元之前)。 Each call returns 20 users list by default , and if you are reaching limit exceed error, I'm sure authenticated user has more than 300 followers. 默认情况下 ,每个调用返回20个用户列表,如果达到限制超过错误,我确定经过身份验证的用户有超过300个关注者。

The solution is to increase the length of user's list received during each call, such that within 15 calls it can fetch all the users. 解决方案是增加每次呼叫期间收到的用户列表的长度,这样在15次呼叫中它可以获取所有用户。 Modify your code as following 修改您的代码如下

for follower in tweepy.Cursor(api.followers, id = me.id, count = 50).items():
    print follower

count denotes the number of users to be fetched per request. count表示每个请求要获取的用户数。

Maximum value of count can be 200, it means within 15 minute, you can only fetch 200 x 15 = 3000 followers, which is enough in general scenarios. count最大值可以是200,这意味着在15分钟内,您只能获取200 x 15 = 3000个粉丝,这在一般情况下就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM