Tweepy 没有返回给定数量的推文

Posted

技术标签:

【中文标题】Tweepy 没有返回给定数量的推文【英文标题】:Tweepy not returning the given amount of tweets 【发布时间】:2021-03-28 18:12:56 【问题描述】:

我有以下代码

api = tweepy.API(auth,wait_on_rate_limit=True)
for tweet in tweepy.Cursor(api.search,
                            tweet_mode="extended",
                            q=query + " exclude:retweets").items(11000):
    hashtags = "#" + " #".join([hashtag['text'] for hashtag in tweet.entities.get('hashtags')])
    print(i)

    if tweet.place:
        tweet_place = tweet.place.full_name + ', ' + tweet.place.country_code
    else:
        tweet_place = "Not Geo-tagged"
    i += 1
    
    csvWriter.writerow([tweet.id, tweet.full_text.encode('utf-8'), tweet.created_at, tweet.lang, tweet.retweet_count, tweet.favorite_count, tweet_place, tweet.user.id, tweet.user.screen_name, tweet.user.followers_count, tweet.user.friends_count, tweet.user.created_at, tweet.user.favourites_count, tweet.user.statuses_count, tweet.user.lang, tweet.user.verified, tweet.user.location])

我正在尝试使用特定搜索查询获取 11000 条推文,但一段时间后它会引发以下错误:

Traceback (most recent call last):
  .............
ConnectionResetError: [Errno 54] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  .............
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  .............
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  .............
tweepy.error.TweepError: Failed to send request: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

早些时候,它发生在大约 2500 条推文中,但在更改查询后,它开始在大约 5000 条推文中发生。知道什么是错误的以及如何解决吗?

【问题讨论】:

【参考方案1】:

这很可能是因为您已经超过了允许的推文数量,您可以一次/每 15 分钟提取一次

查看here了解更多信息。

【讨论】:

是的,但我认为它暂停了一段时间并再次恢复。在大约 2500 条推文中,它暂停了很长一段时间,然后可能因为 wait_on_rate_limit=True 而恢复,但仍然很奇怪,因为这个错误只出现在 2500 和 5000 条推文左右 尝试多次获取 500 条推文,而不是一次抓取所有 10k 条推文 但这样我可能会收到重复的推文。或者您有什么方法可以建议在每个 500 条循环中获得独特的推文? 您对这里的 Twitter API 有什么期望?您可以重复请求(在速率限制内)和一次最多 100 个请求的页面。为什么你认为你可以在一段代码中请求 11000 条推文? 据我所知,如果我重复请求,很可能会收到重复的推文,而我想要 11000 条独特的推文。我主要关心的是让所有推文都不同

以上是关于Tweepy 没有返回给定数量的推文的主要内容,如果未能解决你的问题,请参考以下文章