【发布时间】:2021-05-25 05:28:32
【问题描述】:
我正在尝试在 Twitter 上获取某个用户的所有关注者。大多数用户拥有超过 10 万的关注者。我当前的代码如下:
import tweepy
import time
from ttictoc import tic,toc
key1 = ""
key2 = ""
key3 = ""
key4 = ""
accountvar = ""
auth = tweepy.OAuthHandler(key1, key2)
auth.set_access_token(key3, key4)
tic()
#First, Make sure you have set wait_on_rate_limit to True while connecting through Tweepy
api = tweepy.API(auth, wait_on_rate_limit=True,wait_on_rate_limit_notify=True)
#Below code will request for 5000 follower ids in one request and therefore will give 75K ids in every 15 minute window (as 15 requests could be made in each window).
followerids =[]
for user in tweepy.Cursor(api.followers_ids, screen_name=accountvar,count=5000).items():
followerids.append(user)
print (len(followerids))
#Below function could be used to make lookup requests for ids 100 at a time leading to 18K lookups in each 15 minute window
def get_usernames(userids, api):
fullusers = []
u_count = len(userids)
print(u_count)
try:
for i in range(int(u_count/100) + 1):
end_loc = min((i + 1) * 100, u_count)
fullusers.extend(
api.lookup_users(user_ids=userids[i * 100:end_loc])
)
return fullusers
except:
import traceback
traceback.print_exc()
print ('Something went wrong, quitting...')
#Calling the function below with the list of followeids and tweepy api connection details
fullusers = get_usernames(followerids,api)
print(toc())
很遗憾,我遇到了一个错误。我在 Jupyter Notebook 中使用 Python 3.8。
TweepError: Failed to send request: ('Connection aborted.', OSError("(10054, 'WSAECONNRESET')"))
【问题讨论】:
-
你在哪里得到这个错误?究竟在哪一行代码上? ECONNRESET 表示另一端终止连接。
-
第一次等待后出现错误。
for user in tweepy.Cursor(api.followers_ids, screen_name=accountvar,count=5000).items(): followerids.append(user) -
第一次等待之后 -- 我不知道那是什么意思。你得到了一批 5000 个名字吗?
-
抱歉给您带来了困惑。在这批 75000 个名字之后没有。在获得 75000 个名字后,由于
wait_on_rate_limit = True,有 15 分钟的等待时间。我认为由于等待时间较长,连接会断开。 -
当然。如果不出意外,您的本地路由器/网关可能会在 5 或 10 分钟后超时空闲连接。