我正在尝试使用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循环的行。有点奇怪的是,它并不总是抱怨,甚至不是在一致的时间间隔内出现。有时它会起作用,而其他时候,看起来是随机的,不起作用。
我们尝试在循环中添加较长的休眠时间并减少项目数。