When I hit the Twitter API, I use tweepy because it's easy, Error handling is unexpectedly difficult.
I use it quite often, so I'll leave it as a memo.
When you are getting a lot of tweets in a loop
--For Rate Limit, wait 15 minutes and hit the same API again. --If the specified data cannot be found due to deletion or freezing, you must stop hitting the API.
I just want to branch this difference.
So basically, I'm doing the following: ↓
import datetime
import time
import tweepy
while True:
try:
data = api.search(...)
break
except tweepy.TweepError as e:
print(e)
if e.reason == "[{'message': 'Rate limit exceeded', 'code': 88}]":
print('Error: wait 15 minutes')
#Shows the time when processing stopped even though it waited for 15 minutes.
print(datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
time.sleep(60 * 15)
else:
break
Addendum
It would be nice if this kind of processing was enough, It seems that there was a way to specify it officially. http://docs.tweepy.org/en/latest/api.html I told you in the comment section. Thank you very much.
** e ** was confusing.
print(e)
↓
[{'message': 'Rate limit exceeded', 'code': 88}]
From the result, it looks like an associative array at first glance.
** But it wasn't. ** **
print(e.reason)
print(str(e.reason))
↓
[{'message': 'Rate limit exceeded', 'code': 88}]
<class 'str'>
e.reason was a string! That is, the string [{'message':'Rate limit exceeded','code': 88}]!
That's why
if e.reason == "[{'message': 'Rate limit exceeded', 'code': 88}]":
...
It is processing such as.
I'm pleasure to be of some help.
Recommended Posts