【问题标题】:Tweepy error 104: Connection abortedTweepy 错误 104:连接中止
【发布时间】:2017-11-10 18:44:02
【问题描述】:

我正在尝试使用 Tweepy 抓取一些推文,但在数百个请求后连接崩溃并出现以下错误: tweepy.error.TweepError: 发送请求失败:('Connection aborted.', error("(104, 'ECONNRESET')",))

我的代码是这样的:

  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 捕获错误,但我无法从光标崩溃的位置重新启动光标。 有谁知道如何解决此错误,或从上次已知状态重新启动光标?

谢谢!

【问题讨论】:

    标签: twitter connection tweepy


    【解决方案1】:

    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)
    

    【讨论】:

      【解决方案2】:

      在我看来,tweepy 调用应该在 try 块内。此外,您在 api.search 中有不在 Tweepy API (http://docs.tweepy.org/en/v3.5.0/api.html#help-methods) 中的参数。无论如何,这对我有用:

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

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

      【讨论】:

      • 我遇到了与原始问题相同的错误。你知道在你的解决方案中光标是从它停止的地方开始还是从头开始?在我的情况下,我要求关注者 ID,并且我不想在每次引发错误时从头开始获取 ID。
      • @Miguel:你说得对,一切从头开始。
      猜你喜欢
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      相关资源
      最近更新 更多