从特定用户获取对特定推文的推文回复

Getting tweet replies to a particular tweet from a particular user

我正在尝试浏览特定用户的推文并获取对该推文的所有回复。我发现twitter的APIv1.1并不直接支持

获取特定推文的回复是否有破解或变通方法。我正在使用 python 流媒体 API。

有一个使用 REST 的解决方法 API。

您需要 id_str 和您想要回复的原始推文作者的@用户名。

您应该使用 Search API 作为作者的“@username”。浏览结果以查找 'in_reply_to_status_id' 字段以与您要回复的特定推文的 id_str 进行比较。

这里有一个变通方法,可以使用 tweepy

获取 "username" 发的推文的回复 API

1) 找到需要获取回复的推文tweet_id

2) 使用 api 的搜索方法查询以下内容 (q="@username", since_id=tweet_id) 并检索自 [=19= 以来的所有推文]

3) 匹配 in_reply_to_status_id 到 tweet_id 的结果是 post.

的回复
replies=[] 
non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)  
for full_tweets in tweepy.Cursor(api.user_timeline,screen_name=name,timeout=999999).items(10):
  for tweet in tweepy.Cursor(api.search,q='to:'+name,result_type='recent',timeout=999999).items(1000):
    if hasattr(tweet, 'in_reply_to_status_id_str'):
      if (tweet.in_reply_to_status_id_str==full_tweets.id_str):
        replies.append(tweet.text)
  print("Tweet :",full_tweets.text.translate(non_bmp_map))
  for elements in replies:
       print("Replies :",elements)
  replies.clear()

以上代码将获取用户(姓名)最近的 10 条推文以及对该特定 tweet.The 的回复将保存到名为 replies[=15= 的列表中].您可以通过增加 items 计数(例如:items(100)).

来检索更多推文

即使经过如此多的方法和帮助,我还是花了大约一个小时来找出确切的代码来获取对原作者发布的推文的回复。 twitter 用户除了拉取回复外,大多是回复回复做一个跟帖(这与拉取原作者发的整个跟帖不同)

我最近一直在做一个简单的项目,将原作者线程中每条推文的屏幕截图上传到您的 Google 照片。能够获取 reply 到推文和 reply to the replies

的最重要部分

这是我写的一个简单的递归,它解决了我的问题。此函数使用所有回复和对作者回复的回复的 URL 更新 urls 列表。

def update_urls(tweet, api, urls):
    tweet_id = tweet.id
    user_name = tweet.user.screen_name
    max_id = None
    replies = tweepy.Cursor(api.search, q='to:{}'.format(user_name),
                                since_id=tweet_id, max_id=max_id, tweet_mode='extended').items()

    for reply in replies:
        if(reply.in_reply_to_status_id == tweet_id):
            urls.append(get_twitter_url(user_name, reply.id))
            try:
                for reply_to_reply in update_urls(reply, api, urls):
                    pass
            except Exception:
                pass
        max_id = reply.id
    return urls

如果您打算使用 update_urls 功能,这里有一些您可能需要的附加功能。

def get_api():
    auth=tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_key, access_secret)
    api = tweepy.API(auth, wait_on_rate_limit=True)
    return api

def get_tweet(url):
    tweet_id = url.split('/')[-1]
    api = get_api()
    tweet = api.get_status(tweet_id)
    return tweet

def get_twitter_url(user_name, status_id):
    return "https://twitter.com/" + str(user_name) + "/status/" + str(status_id)

运行确切代码:

api = get_api()
tweet = get_tweet(url)
urls = [url]
urls = update_urls(tweet, api, urls)

如果您想获取特定 URL 的内容,只需调用 get_tweet(url) 并使用 tweet 对象获取 tweet.texttweet.user 等信息.让我知道它是否对您有用:)

以下函数使用用户名和 tweet_id 到 return 对特定 tweet_id 的所有回复文本的列表:(我假设 api 已经在程序中声明。)

def get_tweet_thread(username,tweet_id):
    replies = tweepy.Cursor(api.search, q='to:{}'.format(username),since_id=tweet_id, tweet_mode='extended').items()

    replied_thread = list()
    for reply in replies:
        if(reply._json['in_reply_to_status_id'] == tweet_id):
             replied_thread.append(reply._json['full_text'])
        
    return(replied_thread)