如何在Twitter API v2中正确使用Tweepy Paginator?报错求解
解决Tweepy Paginator调用的AttributeError问题
你遇到的错误核心原因是:把函数调用的结果(Response对象)传给了Paginator,但Paginator需要接收的是可分页的函数对象(比如client.search_all_tweets本身),而非它执行后的返回值。
正确的调用代码
query = '("illegal alien") place_country:US -is:retweet' start_time = '2010-04-06T00:00:00Z' end_time = '2022-12-02T00:00:00Z' # 正确的Paginator用法:传入函数引用,再传递参数 tweets = tweepy.Paginator( client.search_all_tweets, # 这里是函数本身,不要加括号直接调用 query=query, tweet_fields=['context_annotations', 'created_at', 'geo'], place_fields=['place_type','geo'], expansions='geo.place_id', start_time=start_time, end_time=end_time, max_results=100 # 每页最多返回100条推文 ).flatten(limit=200) # 总共获取最多200条推文 for tweet in tweets: print(f"Tweet ID: {tweet.id}\nText: {tweet.text},\nCreated at: {tweet.created_at}\n\n")
关键改动说明
- 把原来直接执行的
client.search_all_tweets(...)替换为client.search_all_tweets,传递函数本身让Paginator内部处理分页调用逻辑。 - 所有原本传给
search_all_tweets的参数,直接以关键字参数的形式传给Paginator,它会在每次分页请求时自动把这些参数传递给目标函数。
内容的提问来源于stack exchange,提问作者Rashid Abramov
相关产品推荐
相关产品推荐

