简体   繁体   中英

Extracting tweets of a specific hashtag using twitter4j

I am able to extract tweets of a specific hashtag using the search method like below

        twitter4j.Twitter twitter =  TwitterFactory.getSingleton();
        Query query = new Query("ipl7");
        QueryResult result = twitter.search(query);
        for (Status status : result.getTweets()) {
            System.out.println("@" + status.getUser().getScreenName() + " : " + status.getText() + " : " + status.getGeoLocation());
        }

But, I got very limited number of tweets using the above method. what should I change to get all the tweets of a specific hashtag?

You can use streaming API to get the recent tweets by a given set of keywords. In your case you have only one keyword which is a hashtag, right? I posted a brief sample code to search tweets by a keyword with Streaming API. You can use both Streaming and Search API for different purposes. Mostly you can use Search API for the hostorical tweets up to a limited time. It allows you to give a date interval. However, you can use Streamin API to catch the recently posted tweets as a tweet stream that contains the keywords that you give.

Example straming code below:

private static void GetTweetStreamForKeywords()
        {
        TwitterStream twitterStream = new TwitterStreamFactory(config).getInstance();

        StatusListener statusListener = new StatusListener() {

         @Override
         public void onStatus(Status status) {
           // The main section that you get the tweet. You can access it by status object.
           // You can save it in a database table.
         }


                @Override
                public void onDeletionNotice(StatusDeletionNotice sdn) {
                    throw new UnsupportedOperationException("Not supported yet."); 
                }

                @Override
                public void onTrackLimitationNotice(int i) {
                    throw new UnsupportedOperationException("Not supported yet."); 
                }

                @Override
                public void onScrubGeo(long l, long l1) {
                    throw new UnsupportedOperationException("Not supported yet."); 
                }

                @Override
                public void onStallWarning(StallWarning sw) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                @Override
                public void onException(Exception ex) {
                    logWriter.WriteErrorLog(ex, "onException()");
                }
            };

            FilterQuery fq = new FilterQuery();        

            String keywords[] = {"sport", "politics", "health"};

            fq.track(keywords);        

            twitterStream.addListener(statusListener);
            twitterStream.filter(fq);          
      }  
package twiter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import twitter4j.GeoLocation;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

public class tweets
{
  public static void main(String[] args) throws Exception 
  {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
      .setOAuthConsumerKey("")
      .setOAuthConsumerSecret("")
      .setOAuthAccessToken("")
      .setOAuthAccessTokenSecret("");
    Twitter twitter = new TwitterFactory(cb.build()).getInstance();
    Query query = new Query("#world");
    int numberOfTweets = 5000;
    long lastID = Long.MAX_VALUE;
    ArrayList<Status> tweets = new ArrayList<Status>();
    while (tweets.size () < numberOfTweets) {
      if (numberOfTweets - tweets.size() > 100)
        query.setCount(100);
      else 
        query.setCount(numberOfTweets - tweets.size());
      try {
        QueryResult result = twitter.search(query);
        tweets.addAll(result.getTweets());
        System.out.println("Gathered " + tweets.size() + " tweets"+"\n");
        for (Status t: tweets) 
          if(t.getId() < lastID) 
              lastID = t.getId();

      }

      catch (TwitterException te) {
        System.out.println("Couldn't connect: " + te);
      }; 
      query.setMaxId(lastID-1);
    }

    for (int i = 0; i < tweets.size(); i++) {
      Status t = (Status) tweets.get(i);

     // GeoLocation loc = t.getGeoLocation();

      String user = t.getUser().getScreenName();
      String msg = t.getText();
      //String time = "";
      //if (loc!=null) {
        //Double lat = t.getGeoLocation().getLatitude();
        //Double lon = t.getGeoLocation().getLongitude();*/
       System.out. println(i + " USER: " + user + " wrote: " + msg + "\n");
      } 
      //else 
        //System.out.println(i + " USER: " + user + " wrote: " + msg+"\n");
    }
  }

Use the count(int resultCount) method :

    Query query = new Query("ipl7");
    query.count(100); //100 is the max allowed
    QueryResult result = twitter.search(query);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM