Tweepy 的 Twitter 错误代码 429
Twitter error code 429 with 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 循环行中。有点奇怪的是它不会每次都抱怨,甚至不会在一致的时间间隔内抱怨。有时会起作用,有时似乎是随机的,但不起作用。
我们已经尝试在循环中添加更长的休眠时间并减少项目数。
您已找到有关错误代码的正确信息。事实上,当由于应用程序的资源速率限制已用尽而无法满足请求时,将返回 429 代码。(来自文档)
我想你的问题不是数据的数量而是频率。
查看 Twitter API rate limits(tweepy 也一样)。
Rate limits are divided into 15 minute intervals. All endpoints require authentication, so there is no concept of unauthenticated calls and rate limits.
There are two initial buckets available for GET requests: 15 calls every 15 minutes, and 180 calls every 15 minutes.
我觉得你可以尝试在这个范围内使用API来避免这个问题
更新
对于最新版本的 Tweepy(从 3.2.0 开始),引入了 *wait_on_rate_limit *。
如果设置为True,它允许自动避免这个问题。
来自文档:
wait_on_rate_limit – Whether or not to automatically wait for rate limits to replenish
在 API 调用中添加 wait_on_rate_limit=True,如下所示:
api = tweepy.API(auth, wait_on_rate_limit=True)
这将使其余代码遵守速率限制
api =tweepy.API(auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True)
这应该有助于设置费率
我正在尝试创建一个使用 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 循环行中。有点奇怪的是它不会每次都抱怨,甚至不会在一致的时间间隔内抱怨。有时会起作用,有时似乎是随机的,但不起作用。
我们已经尝试在循环中添加更长的休眠时间并减少项目数。
您已找到有关错误代码的正确信息。事实上,当由于应用程序的资源速率限制已用尽而无法满足请求时,将返回 429 代码。(来自文档)
我想你的问题不是数据的数量而是频率。
查看 Twitter API rate limits(tweepy 也一样)。
Rate limits are divided into 15 minute intervals. All endpoints require authentication, so there is no concept of unauthenticated calls and rate limits. There are two initial buckets available for GET requests: 15 calls every 15 minutes, and 180 calls every 15 minutes.
我觉得你可以尝试在这个范围内使用API来避免这个问题
更新
对于最新版本的 Tweepy(从 3.2.0 开始),引入了 *wait_on_rate_limit *。
如果设置为True,它允许自动避免这个问题。
来自文档:
wait_on_rate_limit – Whether or not to automatically wait for rate limits to replenish
在 API 调用中添加 wait_on_rate_limit=True,如下所示:
api = tweepy.API(auth, wait_on_rate_limit=True)
这将使其余代码遵守速率限制
api =tweepy.API(auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True)
这应该有助于设置费率