【问题标题】:Twitter error code 429 with Tweepy使用 Tweepy 的 Twitter 错误代码 429
【发布时间】:2017-06-06 19:05:10
【问题描述】:

我正在尝试创建一个使用 tweepy api 访问 twitter 帐户的项目,但我遇到了状态代码 429。现在,我环顾四周,发现这意味着我有太多请求。但是,我一次只能发布 10 条推文,在这些推文中,在我的测试期间应该只存在一条。

for tweet in tweepy.Cursor(api.search, q = '@realtwitchess ',lang = ' ').items(10):
                        try:
                                text = str(tweet.text)
                                textparts = str.split(text) #convert tweet into string array to disect
                                print(text)

                                for x, string in enumerate(textparts): 
                                        if (x < len(textparts)-1): #prevents error that arises with an incomplete call of the twitter bot to start a game
                                                if string == "gamestart" and textparts[x+1][:1] == "@": #find games
                                                        otheruser = api.get_user(screen_name = textparts[2][1:]) #drop the @ sign (although it might not matter)
                                                        self.games.append((tweet.user.id,otheruser.id))
                                        elif (len(textparts[x]) == 4): #find moves
                                                newMove = Move(tweet.user.id,string)
                                                print newMove.getMove()
                                                self.moves.append(newMove)
                                if tweet.user.id == thisBot.id: #ignore self tweets
                                        continue

                        except tweepy.TweepError as e:  
                                print(e.reason)
                                sleep(900)
                                continue
                        except StopIteration: #stop iteration when last tweet is reached
                                break

当错误确实出现时,它位于第一行 for 循环中。有点奇怪的是,它不会每次都抱怨,甚至不会定期抱怨。有时它会起作用,而其他时候,看似随机,却不起作用。

我们已尝试在循环中增加更长的睡眠时间并减少项目数。

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    在 API 调用中添加 wait_on_rate_limit=True,如下所示:

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

    这将使其余代码遵守速率限制

    【讨论】:

      【解决方案2】:

      您找到了有关错误代码的正确信息。实际上,当由于资源的应用程序的速率限制已用尽而无法处理请求时,会返回 429 代码。(来自文档)
      我想您的问题不在于数据量,而在于频率。

      查看 Twitter API rate limits(tweepy 也一样)。

      速率限制分为 15 分钟间隔。所有端点都需要身份验证,因此没有未经身份验证的呼叫和速率限制的概念。 有两个初始存储桶可用于 GET 请求:每 15 分钟调用 15 次,每 15 分钟调用 180 次。

      我觉得你可以尝试在这个范围内使用API​​来避免这个问题

      更新
      对于最新版本的 Tweepy(从 3.2.0 开始),引入了 *wait_on_rate_limit *。
      如果设置为 True,则可以自动避免此问题。

      来自文档:

      wait_on_rate_limit – 是否自动等待速率限制补充

      【讨论】:

      • 可能是的,但您必须设置睡眠以遵守速率限制
      • 我放了 sleep(10) 仍然遇到同样的问题:/
      • 是的,我们需要保存推文并仅在需要时点击 api :)
      【解决方案3】:

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

      这应该有助于设置速率

      【讨论】:

        猜你喜欢
        • 2018-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-31
        • 1970-01-01
        • 2015-04-08
        • 2018-04-27
        • 2021-05-28
        相关资源
        最近更新 更多