java 是否有可能获得超过 100 条推文?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17887984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to get more than 100 tweets?
提问by Nithish Inpursuit Ofhappiness
Is it possible to get more than 100 tweets using the Twitter4j API?
If so can anyone point out the way to do so??
是否可以使用 Twitter4j API 获得 100 多条推文?
如果是这样,任何人都可以指出这样做的方法吗?
回答by Luke
Would need to see your code to provide a code example specific to your case, but you can do this through since_id
or max_id
.
需要查看您的代码以提供特定于您的案例的代码示例,但您可以通过since_id
或来完成此操作max_id
。
This information is for the Twitter API.
此信息适用于 Twitter API。
To get the previous100 tweets:
要获取前100 条推文:
- find the the lowestid in the set that you just retrieved with your query
- perform the same query with the
max_id
option set to the id you just found.
- 找到您刚刚通过查询检索到的集合中最低的id
- 使用
max_id
设置为您刚刚找到的 id的选项执行相同的查询。
To get the next100 tweets:
要获取接下来的100 条推文:
- find the the highestid in the set that you just retrieved with your query
- perform the same query with the
since_id
option set to the id you just found.
- 在您刚刚通过查询检索到的集合中找到最高的id
- 使用
since_id
设置为您刚刚找到的 id的选项执行相同的查询。
In Twitter4j, your Query
object has two fields that represent the above API options: sinceId
and maxId
.
在 Twitter4j 中,您的Query
对象有两个字段代表上述 API 选项:sinceId
和maxId
.
回答by Deniz Ozger
Some Java code that iterates to older pages by using lowest id might look like:
一些使用最低 id 迭代到旧页面的 Java 代码可能如下所示:
Query query = new Query("test");
query.setCount(100);
int searchResultCount;
long lowestTweetId = Long.MAX_VALUE;
do {
QueryResult queryResult = twitterInstance.search(query);
searchResultCount = queryResult.getTweets().size();
for (Status tweet : queryResult.getTweets()) {
// do whatever with the tweet
if (tweet.getId() < lowestTweetId) {
lowestTweetId = tweet.getId();
query.setMaxId(lowestTweetId);
}
}
} while (searchResultCount != 0 && searchResultCount % 100 == 0);
回答by rednoyz
Here is how to get ALL tweets for a user (or at least up to ~3200):
以下是获取用户的所有推文(或至少约 3200 条)的方法:
import java.util.*;
import twitter4j.*;
import twitter4j.conf.*;
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("");
cb.setOAuthConsumerSecret("");
cb.setOAuthAccessToken("");
cb.setOAuthAccessTokenSecret("");
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
int pageno = 1;
String user = "cnn";
List statuses = new ArrayList();
while (true) {
try {
int size = statuses.size();
Paging page = new Paging(pageno++, 100);
statuses.addAll(twitter.getUserTimeline(user, page));
if (statuses.size() == size)
break;
}
catch(TwitterException e) {
e.printStackTrace();
}
}
System.out.println("Total: "+statuses.size());
回答by confucius
You can't load more than 100 tweet per requestbut i don't know why you want this, instead you can load all tweets in "Endless page" i.e loading 10 items each time the user scroll a list .
您不能为每个请求加载超过 100 条推文,但我不知道您为什么要这样做,相反,您可以在“无尽页面”中加载所有推文,即每次用户滚动列表时加载 10 个项目。
for example
例如
Query query = new Query("stackoverflow");
query.setCount(10);// sets the number of tweets to return per page, up to a max of 100
QueryResult result = twitter.search(query);
now if you want to load the next page simple easy
现在,如果您想轻松轻松地加载下一页
if(result.hasNext())//there is more pages to load
{
query = result.nextQuery();
result = twitter.search(query);
}
and so on.
等等。
回答by rishi
To add to Luke's approach Twitter4j does provide pagination to the queries. You can try fetching more than one pages for your query. Set results per page and the page number.
添加到 Luke 的方法中,Twitter4j 确实为查询提供了分页。您可以尝试为您的查询获取多个页面。设置每页结果和页码。
But I suggest first try the since_id
and then try pagination.
但我建议先尝试since_id
,然后再尝试分页。
回答by ohsoifelse
When you get a response containing your first 100 results, you also get the next id with the response. This id can be used as a query parameter "next"= {the id you received from the previous call} while making the call again and it will give you the next 100 tweets.
当您收到包含前 100 个结果的响应时,您还将获得包含响应的下一个 id。这个 id 可以用作查询参数 "next"= {the id you received from previous call},同时再次拨打电话,它将为您提供接下来的 100 条推文。
回答by girip11
For a given query it is possible to extract more than 100 tweets. For a quick demo you can download a twitter GUI application for tweet extraction available at http://preciselyconcise.com/apis_and_installations/tweets_extraction_from_twitter.php.
对于给定的查询,可以提取 100 多条推文。如需快速演示,您可以在http://preciselyconcise.com/apis_and_installations/tweets_extraction_from_twitter.php下载用于推文提取的 twitter GUI 应用程序。
By extracting query results from all available pages for that query, you will be able to extract more than 100 tweets if available under that query. I downloaded the GUI application available in that website and i was able to extract more than 1000 tweets for query #happy.
通过从该查询的所有可用页面中提取查询结果,您将能够提取 100 多条推文(如果在该查询下可用)。我下载了该网站上提供的 GUI 应用程序,并且能够提取 1000 多条推文以进行查询 #happy。