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

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

如果我想从特定用户那里获取最近的推文,我可以只使用 twitter4j 中的用户名

twitter.getUserTimeline(username)

我还可以在全球范围内查找所有符合搜索条件的推文:

twitter.search(new Query(keyword))

有没有一种方法可以在一个用户时间线的所有推文中搜索特定关键字?

是的,使用 Twitter Search API 是可能的,但需要注意的是它只会 return 回溯 7 天的推文。

The Twitter Search API is part of Twitter’s 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. The Twitter Search API searches against a sampling of recent Tweets published in the past 7 days.

Before getting involved, it’s important to know that the Search API is focused on relevance and not completeness. 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.

要使用 Twitter4J API search(query),查询将是 from:<username> <keyword>

流媒体 API 可用于搜索,但不会真正适用,因为它旨在 return 当时来自许多用户的大量推文。

彻底的方法是浏览用户时间轴上的每条推文,然后自己进行搜索。这会涉及更多,但实际上并不那么难。 您将使用 GET statuses/user_timeline API(Twitter4J 中的 getUserTimeline()),然后在 text 字段中搜索您的关键字。