使用Tweepy的Cursor采集推文时提示方法不支持分页报错如何解决
错误原因
你触发报错是Cursor传参逻辑错误导致的:
tweepy.Cursor要求第一个参数传入未执行的API方法对象,方法所需的参数需要单独作为后续参数传给Cursor- 你当前的写法是先执行
api.search_tweets()拿到返回值,再将返回值传给Cursor,不符合调用规范,因此触发分页不支持的报错
正确可运行代码
import tweepy import datetime consumer_key= '替换为你的consumer_key' consumer_secret= '替换为你的consumer_secret' access_token= '替换为你的access_token' access_token_secret= '替换为你的access_token_secret' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # 增加限速等待配置,触发接口频率限制时自动等待,避免直接报错 api = tweepy.API(auth, wait_on_rate_limit=True) try: api.verify_credentials() print("Authentication Successful") except: print("Authentication Error") # 正确的Cursor调用写法:方法不加括号不执行,参数单独传入Cursor # items参数可指定最大采集条数,不填则拉取到接口返回上限 tweets_cursor = tweepy.Cursor(api.search_tweets, q="oranges", tweet_mode='extended', lang='en').items(200) # 字段提取示例,可根据项目需求调整 tweets_dataset = [] for tweet in tweets_cursor: # 如需过滤转发内容可加判断:if not hasattr(tweet, 'retweeted_status') tweets_dataset.append({ "user_id": tweet.user.id_str, "user_name": tweet.user.screen_name, "publish_time": tweet.created_at, "content": tweet.full_text, "retweet_count": tweet.retweet_count, "like_count": tweet.favorite_count }) print(f"采集完成,共获取{len(tweets_dataset)}条有效推文")
注意事项
- 标准版
search_tweets接口仅支持查询最近7天内的公开推文,最多可分页拉取45000条数据 - 如果需要采集更早的推文,需要申请学术版或企业级API权限,调用全量搜索接口实现
内容的提问来源于stack exchange,提问作者Albert the Pro
相关产品推荐
相关产品推荐

