【问题标题】:Tweepy Error while collecting large number of data收集大量数据时出现 Tweepy 错误
【发布时间】: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 分钟后超时空闲连接。

标签: python twitter tweepy


【解决方案1】:

该错误是由于每 75,000 个用户数据后等待时间过长(15 分钟)。由于空闲情况,连接可能会超时。感谢@Tim Roberts 的帮助。

一个解决方案可以是在收集每个 follower_id 并将它们存储在列表中之后使用足够小的睡眠时间time.sleep(.02),如下面的代码 sn-p 所示。所以连接不需要等待很长时间。这样我们就可以设法防止连接断开。

for user in tweepy.Cursor(api.followers_ids, screen_name=accountvar,count=5000).items():
        followerids.append(user)
        time.sleep(.02)
    print (len(followerids))

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 2015-04-09
    • 2022-06-25
    • 2015-01-05
    • 2016-12-11
    • 2019-02-07
    • 2013-12-29
    • 2013-01-01
    • 2018-07-04
    相关资源
    最近更新 更多