我希望使用来自Twitter的Tweety库(python 2.7)的大型列表(超过100k)的关注者ID来获取用户数据。以下是我的代码:
def lookup_user_list(followers_id, api):
full_users = []
users_count = len(followers_id)
try:
for i in range((users_count / 100) + 1):
full_users.extend(api.lookup_users(user_ids=followers_id[i*100:min((i+1)*100, users_count)]))
print 'getting users batch:', i
except tweepy.TweepError as e:
print 'Something went wrong, quitting...', e
time.sleep(15 * 60)
return full_users
该函数的api参数是Tweepy身份验证设置和配置消费者密钥,消费者密钥,访问令牌和访问密钥。
当我这样运行时:
results = lookup_user_list(followers_id, api)
获得448批用户数据后,我收到以下错误:
Something went wrong, quitting... Failed to send request: HTTPSConnectionPool(host='api.twitter.com', port=443): Read timed out.
有没有办法修复此错误并迭代完整的id列表以获取相应的用户数据?
答案 0 :(得分:0)
如果遇到同样的问题,请添加" True:"在尝试之前:在我的代码中解决了这个问题。
def lookup_user_list(followers_id, api):
full_users = []
users_count = len(followers_id)
while True:
try:
for i in range((users_count / 100) + 1):
full_users.extend(api.lookup_users(user_ids=followers_id[i*100:min((i+1)*100, users_count)]))
print 'getting users batch:', i
except tweepy.TweepError as e:
print 'Something went wrong, quitting...', e
time.sleep(15 * 60)
return full_users