简体   繁体   English

如何通过twitter4j中的关键字从特定用户的时间轴搜索推文

[英]How to search tweets from a particular userTimeline by a keyword in twitter4j

If I want to get recent tweets from a particular user I can do that only using the username in twitter4j 如果我想从特定用户那里获取最新的推文,则只能使用twitter4j中的用户名

twitter.getUserTimeline(username)

I can also look globally for all tweets that match a search criteria: 我还可以全局查找与搜索条件匹配的所有推文:

twitter.search(new Query(keyword))

Is there a way I can search within all of the tweets from one user's timeline for a particular keyword? 有什么方法可以在一个用户的时间轴的所有推文中搜索特定关键字?

Yes, it's possible using the Twitter Search API but the caveat is that it will only return tweets going back 7 days. 是的,可以使用Twitter Search API,但需要注意的是,它只会返回7天之前的推文。

The Twitter Search API is part of Twitter's REST API. Twitter搜索API是Twitter REST API的一部分。 It allows queries against the indices of recent or popular Tweets and behaves similarly to, but not exactly like the Search feature available in Twitter mobile or web clients, such as Twitter.com search. 它允许查询最近或流行的Tweets的索引,其行为与Twitter移动或Web客户端(例如Twitter.com搜索)中可用的“搜索”功能类似,但不完全相同。 The Twitter Search API searches against a sampling of recent Tweets published in the past 7 days. Twitter Search API会搜索过去7天内发布的最新Tweet样本。

Before getting involved, it's important to know that the Search API is focused on relevance and not completeness. 在参与之前,重要的是要知道Search API专注于相关性而不是完整性。 This means that some Tweets and users may be missing from search results. 这意味着搜索结果中可能缺少一些推文和用户。 If you want to match for completeness you should consider using a Streaming API instead. 如果要匹配完整性,则应考虑改用Streaming API。

To use the Twitter4J API search(query) the query would be from:<username> <keyword> . 要使用Twitter4J API search(query) ,查询将from:<username> <keyword>

The Streaming API can be used for search but would not really apply as it's designed to return large amounts of tweets from many users at that moment. Streaming API可以用于搜索,但实际上并不适用,因为它旨在从那时返回许多用户的大量推文。

The thorough approach would be to go through every tweet on a user's timeline and do the search yourself. 彻底的方法是遍历用户时间轴上的每条推文,然后自己进行搜索。 This would be much more involved but not that hard actually. 这将涉及更多,但实际上并不那么困难。 You would use the GET statuses/user_timeline API (getUserTimeline() in Twitter4J) and then search your keyword in the text field. 您将使用GET statuss / user_timeline API(Twitter4J中的getUserTimeline()),然后在text字段中搜索关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM