How to retrieve more than 100 results using Twitter4j

前端 未结 4 1792
北荒
北荒 2020-12-01 17:17

I\'m using the Twitter4j library to retrieve tweets, but I\'m not getting nearly enough for my purposes. Currently, I\'m getting that maximum of 100 from one page. How do

4条回答
  •  误落风尘
    2020-12-01 17:26

    Just keep track of the lowest Status id and use that to set the max_id for subsequent search calls. This will allow you to step back through the results 100 at a time until you've got enough, e.g.:

    boolean finished = false;
    while (!finished) {
        final QueryResult result = twitter.search(query);    
    
        final List statuses = result.getTweets();
        long lowestStatusId = Long.MAX_VALUE;
        for (Status status : statuses) {
            // do your processing here and work out if you are 'finished' etc... 
    
            // Capture the lowest (earliest) Status id
            lowestStatusId = Math.min(status.getId(), lowestStatusId);
        }
    
        // Subtracting one here because 'max_id' is inclusive
        query.setMaxId(lowestStatusId - 1);
    }
    

    See Twitter's guide on Working with Timelines for more information.

提交回复
热议问题