You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Tweepy获取特定账号原创推文并排除回复?

如何用Tweepy排除用户时间线中的回复推文?

嗨,我来帮你解决这个问题!你遇到的情况是因为user_timeline接口默认会包含用户的回复内容,不过Tweepy其实提供了现成的参数来过滤掉这些回复。

只需要在调用user_timeline的时候添加exclude_replies=True参数就行啦,另外如果你还想排除转发的话,也可以加上include_rts=False(这个是可选的,看你的实际需求)。

修改后的完整代码如下:

import tweepy

consumer_key = "你的密钥"
consumer_secret = "你的密钥"
access_key = "你的密钥"
access_secret = "你的密钥"

def get_tweets(username):
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_key, access_secret)
    api = tweepy.API(auth)
    
    # 调用user_timeline时添加参数过滤回复,可选排除转发
    tweets = api.user_timeline(screen_name=username, count=200, exclude_replies=True, include_rts=False)
    
    # 后续处理推文的示例逻辑
    for tweet in tweets:
        print(tweet.text)

# 替换成目标用户名后调用函数
get_tweets("目标用户名")

关键参数说明:

  • exclude_replies=True:这个参数会让Tweepy只返回用户原创的推文,自动排除对其他用户的回复内容。
  • include_rts=False:如果不需要获取用户转发的推文,可以加上这个参数,它的默认值是True(即默认包含转发)。

另外要注意,user_timeline的count参数单次最大只能设为200,如果需要获取更多历史推文,你可能需要通过分页的方式循环获取。

内容的提问来源于stack exchange,提问作者Johny Doe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:04:23