Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 超过tweepy.TweepError速率限制 问题:_Python_Twitter_Tweepy - Fatal编程技术网

Python 超过tweepy.TweepError速率限制 问题:

Python 超过tweepy.TweepError速率限制 问题:,python,twitter,tweepy,Python,Twitter,Tweepy,我正在用Python和Tweepy编写一个Twitter机器人。昨晚我成功地让机器人工作,但是,在跟踪了60个人之后,机器人开始抛出一个错误,上面写着[{u'message':u'Rate limited extended',u'code':88}。我知道我只被允许对twitterapi进行一定数量的调用,我发现这显示了我可以对每个函数进行多少次调用。查看代码后,我发现在tweepy.Cursor(api.followers,me.items():中为follower指定时抛出了错误。在我发现的

我正在用Python和Tweepy编写一个Twitter机器人。昨晚我成功地让机器人工作,但是,在跟踪了60个人之后,机器人开始抛出一个错误,上面写着
[{u'message':u'Rate limited extended',u'code':88}
。我知道我只被允许对twitterapi进行一定数量的调用,我发现这显示了我可以对每个函数进行多少次调用。查看代码后,我发现在tweepy.Cursor(api.followers,me.items():中为follower指定
时抛出了错误。在我发现的页面上,写着我收到了多少请求,它说我每15分钟收到15个请求来获得我的追随者。我已经等了一夜,今天早上我重试了代码,但是,它仍然抛出相同的错误。我不明白为什么Tweepy在我应该还有请求的时候抛出了一个超过速率限制的
错误

代码 这是我抛出错误的代码

#!/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}]**
**回溯(最近一次呼叫最后一次):
文件“”,第1行,在
对于api.followers(id=me.id)中的follower:
文件“C:\Users\Lane\AppData\Local\enthught\Canopy\User\lib\site packages\tweepy\binder.py”,第239行,在调用中
返回方法execute()
文件“C:\Users\Lane\AppData\Local\enthught\Canopy\User\lib\site packages\tweepy\binder.py”,执行中第223行
raise TweepError(错误消息,响应)
TweepError:[{u'消息:超过了u'速率限制',u'代码:88}]**
API()。followers
方法实际上是,每个窗口限制15个请求(在下一个历元之前)。每个调用都返回20个用户列表,默认值为default,如果您达到了超出错误的限制,我确信经过身份验证的用户拥有300多个追随者

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

for follower in tweepy.Cursor(api.followers, id = me.id, count = 50).items():
    print follower
count
表示每个请求要获取的用户数

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