How to retrieve more than 100 results using Twitter4j

前端 未结 4 1793
北荒
北荒 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:42

    Here's the function I made for my app based on the past answers. Thank you everybody for your solutions.

    List tweets = new ArrayList();
    
    void getTweets(String term)
    {
    int wantedTweets = 112;
    long lastSearchID = Long.MAX_VALUE;
    int remainingTweets = wantedTweets;
    Query query = new Query(term);
     try
    { 
    
      while(remainingTweets > 0)
      {
        remainingTweets = wantedTweets - tweets.size();
        if(remainingTweets > 100)
        {
          query.count(100);
        }
        else
        {
         query.count(remainingTweets); 
        }
        QueryResult result = twitter.search(query);
        tweets.addAll(result.getTweets());
        Status s = tweets.get(tweets.size()-1);
        firstQueryID = s.getId();
        query.setMaxId(firstQueryID);
        remainingTweets = wantedTweets - tweets.size();
      }
    
      println("tweets.size() "+tweets.size() );
    }
    catch(TwitterException te)
    {
      System.out.println("Failed to search tweets: " + te.getMessage());
      System.exit(-1);
    }
    }
    

提交回复
热议问题