回复特定推文,Twitter API

98

在 Twitter API 中,是否有一种方法可以获取特定推文的回复?谢谢。


也许这个仓库可以帮到你:https://github.com/ScrPzz/twitter_replies_scraper - Alessandro Togni
13个回答

57

以下是获取推文回复的步骤:

  1. 在取回推文时,存储推文ID,即id_str
  2. 使用Twitter搜索API执行以下查询:[q="to:$tweeterusername", sinceId = $tweetId]
  3. 循环遍历所有结果,与in_reply_to_status_id_str to $tweetid匹配的结果是该帖子的回复。

54

据我所了解,目前没有直接实现这个的方法。看起来应该添加这个功能。他们最近添加了一些“转推”功能,似乎也应该添加这个功能。

以下是一种可能的做法,首先使用示例推文数据(来自status/show):

<status>
  <created_at>Tue Apr 07 22:52:51 +0000 2009</created_at>
  <id>1472669360</id>
  <text>At least I can get your humor through tweets. RT @abdur: I don't mean this in a bad way, but genetically speaking your a cul-de-sac.</text>
  <source><a href="http://www.tweetdeck.com/">TweetDeck</a></source>
  <truncated>false</truncated>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <favorited>false</favorited>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <user>
    <id>1401881</id>
     ...

status/show可以找到用户的id。然后statuses/mentions_timeline将返回一个用户的状态列表。只需解析该返回值,查找与原始推文的id相匹配的in_reply_to_status_id


假设用户2回复了用户1的推文。为了从用户1的推文中找出这一点,我需要搜索提到用户1的内容。但是如果我无法作为用户1进行身份验证怎么办?提到的内容难道不是公开可访问的吗? - letronje
@letronje 我不知道有没有其他方法 - 你可以使用搜索API在推文中查找“@user1”,但我认为这不如使用status/mentions可靠。 - Tim Lytle
1
看起来 statues/mentions 已经被弃用了,所以我会解析 search/tweets?q=@screenName 替代它:https://dev.twitter.com/docs/api/1.1/get/search/tweets - Dunc
4
看起来它刚刚被更改为status/mentions_timeline - Tim Lytle
Twitter实验室是一个新的API,未来可能会或可能不会实现此功能:https://twitterdevfeedback.uservoice.com/forums/921790-twitter-developer-labs/suggestions/37629283-build-an-api-to-create-and-retrieve-tweet-threads - Sandro
显示剩余2条评论

19

Twitter API v2现在支持使用conversation_id字段,您可以在文档中了解更多信息。

首先,请求该推文的conversation_id字段。

https://api.twitter.com/2/tweets?ids=1225917697675886593&tweet.fields=conversation_id

其次,使用conversation_id作为查询条件搜索推文。

https://api.twitter.com/2/tweets/search/recent?query=conversation_id:1225912275971657728

这只是一个最简单的例子,所以您应该根据需要添加其他字段到URL中。


2
这将会有效,但是 search/recent 端点仅返回过去 7 天的结果。search/all 端点仅限于具有学术研究访问权限的应用程序。 搜索文档 - Apollo-Roboto
使用这种方法是否可以获取热门回复而不是最近的回复? - Shashank Setty

10

Twitter有一个名为related_results的未记录API。它将为指定的推文ID提供回复。不确定其可靠性如何,因为它是实验性的,但这与在Twitter Web上调用的API调用相同。

使用需自担风险。:)

https://api.twitter.com/1/related_results/show/172019363942117377.json?include_entities=1

欲了解更多信息,请查看dev.twitter上的讨论: https://dev.twitter.com/discussions/293


38
此API已不再活跃。 - mathieu
是的,正如Mathieu所说,它已经不再活跃了。它显示为{u'message': u'抱歉,该页面不存在', u'code': 34}。 - shadab.tughlaq

9

这是我的解决方案。它使用了Abraham的Twitter Oauth PHP库:https://github.com/abraham/twitteroauth

它要求您知道Twitter用户的screen_name属性以及所讨论的推文的id_str属性。通过这种方式,您可以从任何用户的推文中获取任意对话feed:

*更新:刷新代码以反映对象访问与数组访问之间的区别:

function get_conversation($id_str, $screen_name, $return_type = 'json', $count = 100, $result_type = 'mixed', $include_entities = true) {

     $params = array(
          'q' => 'to:' . $screen_name, // no need to urlencode this!
          'count' => $count,
          'result_type' => $result_type,
          'include_entities' => $include_entities,
          'since_id' => $id_str
     );

     $feed = $connection->get('search/tweets', $params);

     $comments = array();

     for ($index = 0; $index < count($feed->statuses); $index++) {
          if ($feed->statuses[$index]->in_reply_to_status_id_str == $id_str) {
               array_push($comments, $feed->statuses[$index]);
          }
     }

     switch ($return_type) {
     case 'array':
          return $comments;
          break;
     case 'json':
     default:
          return json_encode($comments);
          break;
     }

}

3
为什么这个被投票否决了?它完全按照陈述的方式运作,并且准确地回答了问题。此外,我的方法与@vsubbotin不同之处在于,你可以使用任何Twitter用户的ID而不是你自己的。 - lincolnberryiii
4
这很好,但可能会消耗宝贵的速率限制(每个OAuth 180次)。用这种方法运行180条推文...再见! - Mike Barwick

8

在这里,我分享一个简单的 R 代码,用于获取特定推文的回复。

userName = "SrBachchan"

##fetch tweets from @userName timeline
tweets = userTimeline(userName,n = 1)

## converting tweets list to DataFrame  
tweets <- twListToDF(tweets)  

## building queryString to fetch retweets 
queryString = paste0("to:",userName)

## retrieving tweet ID for which reply is to be fetched 
Id = tweets[1,"id"]  

## fetching all the reply to userName
rply = searchTwitter(queryString, sinceID = Id) 
rply = twListToDF(rply)

## eliminate all the reply other then reply to required tweet Id  
rply = rply[!rply$replyToSID > Id,]
rply = rply[!rply$replyToSID < Id,]
rply = rply[complete.cases(rply[,"replyToSID"]),]

## now rply DataFrame contains all the required replies.

4
你可以使用Python中的twarc包来收集一条推文的所有回复。 twarc replies 824077910927691778 > replies.jsonl 此外,还可以使用以下命令收集一条推文的所有回复链(即回复的回复): twarc replies 824077910927691778 --recursive

有没有一种在JavaScript中实现这个的方法? - yashatreya
我不确定,我没有检查,如果发现了什么会让你知道。 - pouria babvey

3

https://gnip.com/ 现在基本上是获取 Twitter 数据的唯一第三方平台。 - abraham

3
我是这样实现的:

1)statuses/update 返回最后一个状态的id(如果 include_entities 为 true) 2)然后您可以请求 statuses/mentions 并通过 in_reply_to_status_id 进行结果过滤。 后者应该等于步骤1中的特定id


2
我几个月前在工作中遇到了同样的问题,因为我之前在REST V1中使用了他们的related_tweets端点。
所以我不得不创建一个解决方法,我在这里记录了下来:
http://adriancrepaz.com/twitter_conversations_api 镜像 - Github分支 这个类应该可以完全满足你的需求。它会抓取移动站点的HTML,并解析对话。我已经使用它一段时间了,看起来非常可靠。
要获取对话...
<?php

require_once 'acTwitterConversation.php';

$twitter = new acTwitterConversation;
$conversation = $twitter->fetchConversion(324215761998594048);
print_r($conversation);

?>

响应

Array
(
    [error] => false
    [tweets] => Array
        (
            [0] => Array
                (
                    [id] => 324214451756728320
                    [state] => before
                    [username] => facebook
                    [name] => Facebook
                    [content] => Facebook for iOS v6.0 ? Now with chat heads and stickers in private messages, and a more beautiful News Feed on iPad itunes.apple.com/us/app/faceboo?
                    [date] => 16 Apr
                    [images] => Array
                        (
                            [thumbnail] => https://pbs.twimg.com/profile_images/3513354941/24aaffa670e634a7da9a087bfa83abe6_normal.png
                            [large] => https://pbs.twimg.com/profile_images/3513354941/24aaffa670e634a7da9a087bfa83abe6.png
                        )
                )

            [1] => Array
                (
                    [id] => 324214861728989184
                    [state] => before
                    [username] => michaelschultz
                    [name] => Michael Schultz
                    [content] => @facebook good April Fools joke Facebook?.chat hasn?t changed. No new features.
                    [date] => 16 Apr
                    [images] => Array
                        (
                            [thumbnail] => https://pbs.twimg.com/profile_images/414193649073668096/dbIUerA8_normal.jpeg
                            [large] => https://pbs.twimg.com/profile_images/414193649073668096/dbIUerA8.jpeg
                        )
                )
             ....             
        )
)

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