java - twitter4j 搜索完整的 7 天范围
问题描述
我尝试使用关键字保存推文,我知道免费 API 只提供 7 天的结果,但它从来没有得到任何超过几个小时的时间线,有时它甚至给我一个小时的范围。我确实将 since() 和 until() 设置为搜索查询。我从一次运行中获得的最大推文数量少于 400 条。谁能告诉我为什么它会自动停止,结果如此之少?谢谢。
public static void main(String[] args) throws TwitterException {
String KEY_word;
String Exclude;
String Since;
String Until;
String OPT_dir;
String time;
int x;
Propertyloader confg = new Propertyloader();
KEY_word = confg.getProperty("KEY_word");
Exclude = confg.getProperty("Exclude");
Since = confg.getProperty("Since");
Until = confg.getProperty("Until");
OPT_dir = confg.getProperty("OPT_dir");
Twitter twitter = new TwitterFactory().getInstance();
try {
time = new SimpleDateFormat("yyyyMMddHHmm'.txt'").format(new Date());
x = 1;
Query query = new Query(KEY_word + Exclude);
query.since(Since);
query.until(Until);
QueryResult result;
do {
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
try {
String filedir = OPT_dir + KEY_word + time;
writeStringToFile(filedir, x + ". " + "@" + tweet.getUser().getScreenName() + ", At: " + tweet.getCreatedAt() + ", Rt= " + tweet.getRetweetCount() + ", Text: " + tweet.getText());
x += 1;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} while ((query = result.nextQuery()) != null);
System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}
}
public static void writeStringToFile(String filePathAndName, String stringToBeWritten) throws IOException{
try
{
String filename= filePathAndName;
boolean append = true;
FileWriter fw = new FileWriter(filename,append);
fw.write(stringToBeWritten);//appends the string to the file
fw.write("\n" +"\n");
fw.close();
}
catch(IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
}
}
解决方案
您可以使用 setMaxId 获取更多推文。这是一个例子:
long lowestTweetId = Long.MAX_VALUE;
x = 1;
Query query = new Query("stackoverflow");
query.since("2018-08-10");
query.until("2018-08-16");
query.setCount(100); //The number of tweets to return per page, up to a maximum of 100. Defaults to 15. https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html
query.setResultType(Query.RECENT); // to get an order
int searchResultCount=100;
QueryResult result;
do {
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
try {
System.out.println( "@" + tweet.getUser().getScreenName() + ", At: " + tweet.getCreatedAt() );
x += 1;
if (tweet.getId() < lowestTweetId) {
lowestTweetId = tweet.getId();
query.setMaxId(lowestTweetId-1);
}
else {// each new maxid should be smaller than the other one so break here
//do whatever you want to handle it ex: break from two loops
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} while (searchResultCount != 0 );
推荐阅读
- mysql - 无法启动 MySQL 服务
- python - 在py2app中隐藏主要的Python文件
- javascript - 如何在javascript函数中改变给定的参数
- git - 错误:无法锁定配置文件 C:/Users/admin/Desktop/%Admin%/.gitconfig: 没有这样的文件或目录
- python - ERROR selenium 常见异常 WebDriverException
- node.js - 如何在 NodeJs 中一个接一个地执行一个函数?
- cmake - 在 CMake 中转发无值函数参数的简单方法
- django - Django 和 postgres 时区转换为 UTC
- python - 如何在pygame中为不同的Rect Obj反弹具有不同坐标列表的单独矩形?
- php - 安装作曲家 PHP 警告:file_get_contents(D:/software/PHP/php-7.1.1/extras/ssl):无法打开流:权限被拒绝