首页 > 解决方案 > 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());
    }
}

标签: javasearchtwittertwitter4jtimeline

解决方案


您可以使用 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 );

推荐阅读