如何使用 Tweepy 检索特定用户的推文回复/评论?

发布于 2025-01-17 02:28:06 字数 723 浏览 3 评论 0原文

这是我尝试过的代码,我想要一个除此之外的解决方案

def replies_from_tweet_id(tweet_id,username):
  replies=[]
  for tweet in tweepy.Cursor(api.search,q='to:'+username, result_type='recent',timeout=999999,tweet_mode='extended',count=3000).items():
    if hasattr(tweet, 'in_reply_to_status_id_str'):
      if (tweet.in_reply_to_status_id_str==str(tweet_id)):
          replies.append(tweet)

  
  with open('./Data/'+username+'/'+tweet_id+'.csv', 'w') as f:
    csv_writer = csv.DictWriter(f, fieldnames=('text','date'))
    csv_writer.writeheader()
    for tweet in replies[:200]:
        row = {'text': tweet.full_text.replace('\n', ' ').strip('@'+username),'date':tweet.created_at }
        csv_writer.writerow(row)

This is the code I've tried and I want a solution apart from this one

def replies_from_tweet_id(tweet_id,username):
  replies=[]
  for tweet in tweepy.Cursor(api.search,q='to:'+username, result_type='recent',timeout=999999,tweet_mode='extended',count=3000).items():
    if hasattr(tweet, 'in_reply_to_status_id_str'):
      if (tweet.in_reply_to_status_id_str==str(tweet_id)):
          replies.append(tweet)

  
  with open('./Data/'+username+'/'+tweet_id+'.csv', 'w') as f:
    csv_writer = csv.DictWriter(f, fieldnames=('text','date'))
    csv_writer.writeheader()
    for tweet in replies[:200]:
        row = {'text': tweet.full_text.replace('\n', ' ').strip('@'+username),'date':tweet.created_at }
        csv_writer.writerow(row)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沒落の蓅哖 2025-01-24 02:28:06

使用 Twitter API v2,您可以通过 对话 ID 查询从特定对话线程中检索推文。

With Twitter API v2, you can query by conversation ID to retrieve Tweets from a specific conversation thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文