首页 > 解决方案 > Python 代码不等待 Tweepy wait_on_rate_limit

问题描述

我正在使用 Tweepy Twitter API 和 Python 使用以下代码检索相当多的推文(约 40,000 条):

def getTweets(query, num):
#gets the specified number of tweets
_max_queries = 100

tweets = tweet_batch = api.search(q=query, count=num, tweet_mode='extended')
ct = 1
while len(tweets) < num and ct < _max_queries:
    print("Got %d tweets!" % (len(tweets)))
    tweet_batch = api.search(q=query, count=num-len(tweets), max_id=tweet_batch.max_id, tweet_mode='extended')
    tweets.extend(tweet_batch)
    ct += 1
return tweets

我将 api wait_on_rate_limit 设置为 true。在我调用 getTweets() 函数后,我立即尝试处理推文。唯一的问题是 Python 没有等待 Tweepy 的速率限制重置,而是过早地继续执行进程代码。例如,如果我尝试提取 20,000 条推文,Tweepy 会提取大约 9,000 条并立即处理它们,而不是等待速率限制重置,然后再提取其余的推文。有什么建议么?

标签: pythonpython-3.xapitwittertweepy

解决方案


推荐阅读