我可以使用搜索方法提取特定主题标签的推文,如下所示
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());
}
但是,使用上述方法我的推文数量非常有限。我应该更改什么来获取特定主题标签的所有推文?
答案 0 :(得分:3)
您可以使用流API通过一组给定的关键字获取最近的推文。在您的情况下,您只有一个关键字是一个标签,对吗?我发布了一个简短的示例代码,用于通过Streaming API的关键字搜索推文。您可以将Streaming和Search API用于不同目的。大多数情况下,您可以在有限的时间内将搜索API用于主题推文。它允许您给出日期间隔。但是,您可以使用Streamin API将最近发布的推文作为包含您提供的关键字的推文流来捕获。
以下示例拉线代码:
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);
}
答案 1 :(得分:1)
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");
}
}
答案 2 :(得分:0)
Query query = new Query("ipl7");
query.count(100); //100 is the max allowed
QueryResult result = twitter.search(query);