微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

错误 10054:远程主机强行关闭现有连接 - Tweepy 错误

如何解决错误 10054:远程主机强行关闭现有连接 - Tweepy 错误

我正在尝试从拥有近 60 万粉丝的 Twitter 帐户中检索有关粉丝的信息。我能够使用此代码在另一个帐户上检索大约 280,000 个关注者,没有任何问题。但是对于更大的帐户,我收到此错误

[Errno 10054] 远程主机强行关闭现有连接

我确定没有两个开放的连接。我正在使用等待限制参数。

  1. 你知道我需要做什么才能获得全部 60 万左右的粉丝吗?

  2. 有没有办法编辑代码,以便在发生该错误时将检索到的关注者写入 CSV?或者我可以为每个循环将检索到的数据写入 CSV。现在,一旦代码出错,我就会丢失它检索到的所有内容,而且我知道它在出错之前检索了数十万粉丝。如果我能保留这些数据会很有帮助。

我正在使用的代码是从 [Retrieve Twitter Followers - Brianna Herold][1] 中检索到的:

import pandas as pd

# Twitter API credentials
consumer_key = '***'
consumer_secret = '***'
access_token = '***'
access_secret = '***'

auth = OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_secret)
api = API(auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True)

screen_name = '***'
ids = []
for fid in Cursor(api.followers_ids,screen_name=screen_name,count=5000).items():
    ids.append(fid)

info = []
for i in range(0,len(ids),100):
    try:
        chunk = ids[i:i+100]
        info.extend(api.lookup_users(user_ids=chunk))
    except:
        import traceback
        traceback.print_exc()
        print('Something went wrong,skipping...')

data = [x._json for x in info]
df = pd.DataFrame(data)
df = df[['id','name','screen_name','location','description','url','followers_count','friends_count','created_at','verified']]
df.to_csv(r' myfilepath)


  [1]: https://towardsdatascience.com/how-to-download-twitter-friends-or-followers-for-free-b9d5ac23812

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。