我试着用关键字保存tweet,我知道免费API只给出了7天的结果,但它从来没有得到超过几个小时的时间线,有时甚至给我一个小时的范围。我确实对搜索查询设置了自开始()和直到()。我从一次运行中得到的推文的最大数量不到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());
    }
}发布于 2018-08-16 21:13:14
您可以使用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 );https://stackoverflow.com/questions/51461872
复制相似问题