我尝试在while - try - except循环中捕获TweepError异常但不成功。当出现TweepError / RateLimitError时,以下代码将一直停止运行。
import tweepy
import time
name_set = ('name1','name2','name3')
result = []
for screen_name in name_set:
while True:
profile = api.get_user(screen_name = screen_name)
try:
print('collecting user %s'%screen_name)
result.append(profile)
break
except tweepy.RateLimitError:
print('sleep 15 minutes')
sleep(900)
continue
except tweepy.TweepError as e:
print(e)
print('Account %s'%screen_name)
break
else:
print('Account %s'%screen_name)
break
TweepError
TweepError:[{'message':'找不到用户。','代码':50}]
答案 0 :(得分:0)
您应该在try
块中放置一个API调用语句来捕获异常:
try:
profile = api.get_user(screen_name = screen_name)
print('collecting user %s'%screen_name)
...