gpt4 book ai didi

python - 使用 tweepy 时出现此错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:56:45 26 4
gpt4 key购买 nike

用于收集推文并将其发送到 csv 文件的 Python 代码

不断返回错误

tweepy.error.RateLimitError: [{'code': 88, 'message': 'Rate limit exceeded'}]

尝试获取最新的时间线并将所有这些推文发送到 csv 文件

谢谢你的帮助

def get_all_tweets(screen_name):

#authorize twitter, initialize tweepy
auth = tweepy.OAuthHandler(consumer_token, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)

#initialize a list to hold all the tweepy Tweets
alltweets = []


new_tweets = api.home_timeline (screen_name = screen_name,count=20)

#save most recent tweets
alltweets.extend(new_tweets)

#save the id of the oldest tweet less one
oldest = alltweets[-1].id - 1

while len(new_tweets) > 0:
print ("getting tweets before %s" % (oldest))

#all subsiquent requests use the max_id param to prevent duplicates
new_tweets = api.home_timeline(screen_name = screen_name,count=20,max_id=oldest)

#save most recent tweets
alltweets.extend(new_tweets)

#update the id of the oldest tweet less one
oldest = alltweets[-1].id - 1

print ("...%s tweets downloaded so far" % (len(alltweets)))

#transform the tweepy tweets into a 2D array that will populate the csv
outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets]

#write the csv
with open('%s_tweetsBQ.csv' % screen_name, 'w') as f:
writer = csv.writer(f)
writer.writerow(["id","created_at","text"])
writer.writerows(outtweets)

pass


if __name__ == '__main__':
#pass in the username of the account you want to download
get_all_tweets("BQ")

最佳答案

您的代码没问题,您刚刚达到 Twitter Streaming API 限制。让您再次提取推文大约需要一个小时。

你应该在初始化 tweetpy 时添加 wait_on_rate_limit=True 选项:

api = tweepy.API(auth, wait_on_rate_limit=True)

我强烈建议您看一下:https://dev.twitter.com/rest/public/rate-limiting了解更多详情。

关于python - 使用 tweepy 时出现此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38775997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com