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

17

我正在尝试查看特定用户的推文,并获取该推文的所有回复。我发现 Twitter 的 API v1.1 并不直接支持此功能。

是否有一种方法或解决方案可以获取特定推文的回复?我正在使用 Python Streaming API。

5个回答

23

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

您需要原始推文作者的id_str和@username,以便查找回复的原始推文。

您应该使用搜索API查找原始推文作者的“@username”。浏览结果,查找'in_reply_to_status_id'字段并将其与要查找回复的特定推文的id_str进行比较。


13
你能用编程的方式来写吗? - davdis

6

以下是使用 tweepy 的 rest API 获取“用户名”发表的推文回复的解决方法:

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

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

3)与 tweet_id 匹配的 in_reply_to_status_id 就是该帖子的回复。


4
你如何编写这个程序? - davdis

6
我找到了获取原作者推文回复的准确代码。除了获取回复外,Twitter用户通常会回复回复以形成线程(这使得获取原作者创建的整个线程与众不同)。
以下是我编写的一个简单递归函数,可以解决我的问题。该函数会更新urls列表,其中包含作者回复及其回复的所有URL。
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等信息。


5
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条最近推文以及对该特定推文的回复。回复将保存到名为replies的列表中。您可以通过增加items计数来检索更多推文(例如:items(100))。


1
以下函数使用用户名和推文 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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接