Twitter Tweepy错误104:连接中止

Twitter Tweepy错误104:连接中止,twitter,connection,tweepy,Twitter,Connection,Tweepy,我正试图使用Tweepy抓取一些tweet,但在数百次请求后连接崩溃,出现以下错误: tweepy.error.TweepError: 发送请求失败:(“连接已中止”,错误(((104,“EconReset”),) 我的代码如下: for status in tweepy.Cursor(api.search, q="", count=100,

我正试图使用Tweepy抓取一些tweet,但在数百次请求后连接崩溃,出现以下错误: tweepy.error.TweepError: 发送请求失败:(“连接已中止”,错误(((104,“EconReset”),)

我的代码如下:

  for status in tweepy.Cursor(api.search,
                           q="",
                           count=100,
                           include_entities=True,
                           monitor_rate_limit=True, 
                           wait_on_rate_limit=True,
                           wait_on_rate_limit_notify = True,
                           retry_count = 5, #retry 5 times
                           retry_delay = 5, #seconds to wait for retry
                           geocode ="34.0207489,-118.6926066,100mi", # los angeles
                           until=until_date,
                           lang="en").items():

      try:
        towrite = json.dumps(status._json)
        output.write(towrite + "\n")
      except Exception, e:
        log.error(e)
      c+=1
      if c % 10000 == 0:  # 100 requests, sleep
        time.sleep(900) # sleep 15 min
我可以使用try/except捕获错误,但无法从光标崩溃的位置重新启动光标。 是否有人知道如何解决此错误,或从上次已知的状态重新启动光标


谢谢

Tweepy文档称,请求/15分钟窗口(用户身份验证)为180,但显然睡眠时间过长会影响连接可靠性(在一些请求之后),因此,如果每5秒运行一次请求,一切似乎都正常:

   for status in tweepy.Cursor(api.search,
                       q="",
                       count=100,
                       include_entities=True,
                       monitor_rate_limit=True, 
                       wait_on_rate_limit=True,
                       wait_on_rate_limit_notify = True,
                       retry_count = 5, #retry 5 times
                       retry_delay = 5, #seconds to wait for retry
                       geocode ="34.0207489,-118.6926066,100mi", # los angeles
                       until=until_date,
                       lang="en").items():

  try:
    towrite = json.dumps(status._json)
    output.write(towrite + "\n")
  except Exception, e:
    log.error(e)
  c+=1
  if c % 100 == 0:  # first request completed, sleep 5 sec
    time.sleep(5)

在我看来,
tweepy
调用应该在
try
块中。此外,api.search中的参数不在Tweepy api()中。不管怎样,这对我来说很有效:

backoff\u计数器=1
尽管如此:
尝试:
对于tweepy.Cursor(api.search,q=“test”).items()中的my_项:
#对我的物品做些什么
打破
除tweepy.TweepError为e外:
打印(如原因)
睡眠(60*backoff_计数器)
后退计数器+=1
持续

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

我遇到了与原始问题相同的错误。您知道在您的解决方案中,光标是从停止的位置开始,还是从开始开始?在我的情况下,我请求的是追随者ID,我不想每次出现错误时都从头开始获取ID。@Miguel:你说得对,它是从头开始的。