【问题标题】:Gracefully handle errors and exceptions for user_timeline method in Tweepy优雅地处理 Tweepy 中 user_timeline 方法的错误和异常
【发布时间】:2015-02-05 16:59:24
【问题描述】:

我正在为大量用户收集推文,因此脚本将在无人监督的情况下运行数天/数周。 我在big_list 中有一个 user_ids 列表。 我认为一些推文是私人的,我的脚本停止了,所以我想要一种方法让脚本继续到下一个 user_id(并可能打印一条警告消息)。

我还想要关于如何使其对其他错误或异常具有鲁棒性的建议(例如,让脚本在错误或超时时休眠)

这是我所拥有的总结:

import tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
my_api = tweepy.API(auth)

for id_str in big_list:
    all_tweets = get_all_tweets(id_str=id_str, api=my_api)
    #Here: insert some tweets into my database

get_all_tweets 函数抛出错误,基本上是重复调用:

my_api.user_timeline(user_id = id_str, count=200)

以防万一,它给出的回溯如下:

/home/username/anaconda/lib/python2.7/site-packages/tweepy/binder.pyc in execute(self)
    201                 except Exception:
    202                     error_msg = "Twitter error response: status code = %s" % resp.status
--> 203                 raise TweepError(error_msg, resp)
    204 
    205             # Parse the response payload

TweepError: Not authorized.

如果您需要更多详细信息,请告诉我。谢谢!

----------- 编辑 --------

This question 有一些信息。

我想我可以尝试针对不同类型的错误执行try/except 块?我不知道所有相关内容,因此将不胜感激具有现场经验的人的最佳实践!

--------- 编辑 2 --------

我收到了一些Rate limit exceeded errors,所以我让循环像这样休眠。 else 部分将处理“未授权”错误和其他一些(未知?)错误。不过,这仍然让我失去了big_list 中的一个元素。

for id_str in big_list:
    try:
        all_tweets = get_all_tweets(id_str=id_str, api=my_api)
        # HERE: save tweets
    except tweepy.TweepError, e:
        if e == "[{u'message': u'Rate limit exceeded', u'code': 88}]":
            time.sleep(60*5) #Sleep for 5 minutes
        else:
            print e

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    你可以做一个“通过”:

    import tweepy
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    my_api = tweepy.API(auth)
    
    for id_str in big_list:
        try:
            all_tweets = get_all_tweets(id_str=id_str, api=my_api)
        except Exception, e:
             pass
    

    【讨论】:

    • 感谢您的回答。您的解决方案有效,但我也在寻找一种处理其他相关异常的方法。我编辑了我的问题以添加其中一个(超出速率限制)。对于“未授权”,我可以继续循环,但是对于超过速率限制,我需要 time.sleep() 就可以了,否则我会一直被拒绝。
    • 这应该适用于不同类型的异常stackoverflow.com/a/17168626/2838313当你得到费率异常时,你可以睡规定的时间! (同时保存循环的当前状态)
    【解决方案2】:

    我真的迟到了,但这些天我遇到了同样的问题。由于需要 time.sleep() 我解决了这个问题,感谢alecxe reply to this question

    我在过去潜水,但我希望这对将来的人有所帮助。

    【讨论】:

      【解决方案3】:

      您可以在创建 API 对象时只使用 Tweepy 的“wait_on_ratelimit”和“wait_on_rate_limit_notify”,并添加一般的 tweepy 错误处理,然后,在显示特定错误的情况下,您可以尝试个性化处理每个错误的代码。应该是这样的:

      import tweepy 
      auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
      auth.set_access_token(access_token, access_token_secret) 
      my_api = tweepy.API(auth, wait_on_rate_limit = True, wait_on_rate_limit_notify = True)
      
      for id_str in big_list: 
          try: 
              all_tweets = get_all_tweets(id_str=id_str, api=my_api) 
          except tweepy.TweepError as e: 
              print("Tweepy Error: {}".format(e))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-10
        • 1970-01-01
        • 2017-03-04
        • 2014-07-10
        • 1970-01-01
        • 2016-07-30
        • 1970-01-01
        • 2015-12-14
        相关资源
        最近更新 更多