Twitter错误代码429与Tweepy

时间:2017-01-22 01:59:03

标签: python twitter tweepy

我正在尝试使用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循环行中。有点奇怪的是,它不是每次都抱怨,甚至是一致的间隔。有时它会起作用,而其他时候,似乎是随机的,不起作用。

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

3 个答案:

答案 0 :(得分:14)

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

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

这将使其余代码服从速率限制

答案 1 :(得分:8)

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

检查Twitter API rate limits(这与tweepy相同)。

  

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

我认为您可以尝试在此范围内使用API​​来避免此问题

答案 2 :(得分:0)

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

这应该有助于设置费率