使用Tweepy调用Twitter API未返回created_at等字段问题求助
问题原因及解决办法
这是因为Tweepy调用的Twitter API v2默认只返回最基础的推文核心字段(也就是你看到的edit_history_tweet_ids、id、text),像created_at、geo这类属于扩展字段,必须显式指定才能获取到。
要获取这些缺失的字段,你需要在search_all_tweets方法里添加tweet_fields参数,明确列出你需要的字段:
修改后的代码示例:
query = '$MSFT' start_time = '2019-01-01T00:00:00Z' end_time = '2019-02-01T00:00:00Z' max_results = 10 # 指定需要的推文字段,这里包含created_at和geo results = client.search_all_tweets( query=query, max_results=max_results, start_time=start_time, end_time=end_time, tweet_fields=['created_at', 'geo'] )
如果还需要其他相关数据,比如推文作者的信息、地点详情,还可以搭配其他参数:
- 用
user_fields指定用户相关字段(比如username、location) - 用
place_fields指定地点相关字段(比如full_name、country),同时需要加上expansions='geo.place_id'来关联地点数据
内容的提问来源于stack exchange,提问作者Tes Taccount
相关产品推荐
相关产品推荐

