我正在为大量用户收集tweets,因此脚本将在无人监督的情况下运行数天/数周。
我在big_list
中有一个用户id列表。
我认为有些tweets是私有的,我的脚本停止了,所以我想让脚本继续到下一个用户I d(可能还会打印一条警告消息)。
我还想建议如何使它对其他错误或异常健壮(例如,脚本在错误或超时时休眠)
这是我的总结:
import tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
my_api = tweepy.API(auth)
for id_str in big_list:
all_tweets = get_all_tweets(id_str=id_str, api=my_api)
#Here: insert some tweets into my database
get_all_tweets
函数抛出错误,它基本上重复调用:
my_api.user_timeline(user_id = id_str, count=200)
以防万一,它给出的回溯如下:
/home/username/anaconda/lib/python2.7/site-packages/tweepy/binder.pyc in execute(self)
201 except Exception:
202 error_msg = "Twitter error response: status code = %s" % resp.status
--> 203 raise TweepError(error_msg, resp)
204
205 # Parse the response payload
TweepError: Not authorized.
如果你需要更多的细节,请告诉我。谢谢!
———————————————————
This question有一些信息。
我想我可以尝试为不同类型的错误创建一个try/except
块?我不知道所有相关的,所以最好的做法,有实地经验的人将不胜感激!
—————————————————————————
我得到了一些Rate limit exceeded errors
所以我让循环像这样睡觉。else
部分将处理“未授权”错误和其他一些(未知?)错误。但这仍然使我在big_list
中丢失了一个元素。
for id_str in big_list:
try:
all_tweets = get_all_tweets(id_str=id_str, api=my_api)
# HERE: save tweets
except tweepy.TweepError, e:
if e == "[{u'message': u'Rate limit exceeded', u'code': 88}]":
time.sleep(60*5) #Sleep for 5 minutes
else:
print e
你可以做一个“通行证”:
我真的迟到了,但这几天我也遇到了同样的问题。由于时间的需要,我通过alecxe reply to this question解决了这个问题。
我在潜水过去,但我希望这将有助于在未来的人。
相关问题 更多 >
编程相关推荐