【发布时间】:2022-03-19 00:26:46
【问题描述】:
我正在尝试从拥有近 60 万关注者的 Twitter 帐户中检索关注者信息。我能够使用此代码在另一个帐户上检索大约 280,000 名关注者,没有任何问题。但是对于更大的帐户,我会收到此错误:
[Errno 10054] 现有连接被远程主机强行关闭
我确定我没有两个打开的连接。我正在使用等待限制参数。
-
你知道我需要做什么才能获得全部 60 万左右的关注者吗?
-
有没有办法编辑代码,以便在出现该错误时将检索到的关注者写入 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
【问题讨论】:
-
你找到解决办法了吗?还是什么原因造成的?