如何解决除非语句不会返回到For循环Python tweepy
我有以下代码。对于拥有超过75,000个关注者的Twitter帐户,我总是会收到此错误:
tweepy.error.TweepError: Failed to send request: ('Connection aborted.',ConnectionResetError(10054,'An existing connection was forcibly closed by the remote host',None,10054,None))
这就是为什么我在代码中添加了except块的原因,如 Getting back to back error using "wait_on_rate_limit" parameter
对于拥有超过75,000个关注者的任何帐户,我都会收到该错误,它将满足except子句,然后理想情况下,它应该“继续”回到循环中并获取其余关注者。但是它不会,那时候它会跳出循环并停止。 我不明白我在想什么?
我的代码:
for entity in entities:
try:
c = tweepy.Cursor(api.followers_ids,screen_name=entity,count=5000)
for pages in c.pages():
id.append(pages)
et.append(entity)
d = {'follower_id': id[i],'name': et[i]}
df_temp=pd.DataFrame(d)
df = df.append(df_temp)
print('Loop NUmber:',i)
i=i+1
print(df.shape)
except tweepy.TweepError as e:
print(e.reason)
time.sleep(60 * backoff_counter)
backoff_counter += 1
continue
请告诉我我在这里想念什么?
解决方法
这不是答案,但可以帮助缩小问题范围。
尝试此代码。它将迭代器存储在列表中,以便它们(希望)结束。它还会保留实体和页面的计数。
i=0
id = []
et = []
backoff_counter = 1
entCtr = 0
while True:
try:
lstEntities = list(entities) # lock entity list
print('Entity Length',len(lstEntities))
for entity in lstEntities:
entCtr += 1
print('entCtr',entCtr)
c = tweepy.Cursor(api.followers_ids,screen_name=entity,count=5000)
lstPages = list(c.pages()) # freeze here ?
print('entCtr',entCtr,'Page Length',len(lstPages))
i = 0
for pages in lstPages:
id.append(pages)
et.append(entity)
d = {'follower_id': id[i],'name': et[i]}
df_temp=pd.DataFrame(d)
df = df.append(df_temp)
print('Page Loop Number:',i)
i=i+1
print(df.shape)
break
except tweepy.TweepError as e:
print(e.reason)
time.sleep(60 * backoff_counter)
backoff_counter += 1
continue
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。