使用" wait_on_rate_limit"回退错误参数

时间:2017-07-04 21:42:27

标签: python twitter tweepy

为了避免速率限制错误,我使用了参数:

wait_on_rate_limit

在功能

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

起初我的程序运行正常。当我超过速率限制时,我收到了消息:
      "达到了费率限制。睡觉时间:909"。我的程序睡了这么长时间,然后我的程序继续收集数据。在某些时候,我得到了一些背靠背的错误。

...
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

...
urllib3.exceptions.ProtocolError: ('Connection aborted.', 
ConnectionResetError(10054, 'An existing connection was forcibly closed by 
the remote host', None, 10054, None))

During handling of the above exception, another exception occurred:

...
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

During handling of the above exception, another exception occurred:

...
tweepy.error.TweepError: Failed to send request: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

我的代码:

for user in tweepy.Cursor(api.friends, id="twitter").items():
    friendsOfUser=user.screen_name
    ## Do something with friendsOfUser

我能做些什么吗?

2 个答案:

答案 0 :(得分:0)

主机关闭连接的事实无法执行任何操作。如果您正在等待速率限制,我打赌您在API使用方面有点激进:)尝试捕获TweepError并明确等待一段时间再次尝试。

您可以尝试这样的事情:

import time

...
try:
    for user in tweepy.Cursor(api.friends, id="twitter").items():
        friendsOfUser=user.screen_name
        ...
except tweepy.TweepError:
    time.sleep(120) # sleep for 2 minutes. You may try different time

答案 1 :(得分:0)

这对我有用:

    backoff_counter = 1
    while True:
        try:
            for user in tweepy.Cursor(api.friends, id="twitter").items():
                # do something with user
            break
        except tweepy.TweepError as e:
            print(e.reason)
            sleep(60*backoff_counter)
            backoff_counter += 1
            continue

基本上,当你得到错误时,你会睡一会儿,然后再试一次。我使用增量退避来确保睡眠时间足以重新建立连接。