Java 使用 twitter4j 提取特定主题标签的推文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23341215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extracting tweets of a specific hashtag using twitter4j
提问by yAsH
I am able to extract tweets of a specific hashtag using the searchmethod 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?
但是,使用上述方法我得到的推文数量非常有限。我应该更改什么才能获取特定主题标签的所有推文?
回答by ben75
Use the count(int resultCount)method :
Query query = new Query("ipl7");
query.count(100); //100 is the max allowed
QueryResult result = twitter.search(query);
回答by mgokhanbakal
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.
您可以使用流 API 通过给定的一组关键字获取最近的推文。在您的情况下,您只有一个关键字是主题标签,对吗?我发布了一个简短的示例代码,用于使用 Streaming API 通过关键字搜索推文。您可以将 Streaming API 和 Search API 用于不同目的。大多数情况下,您可以在有限的时间内将搜索 API 用于主机推文。它允许您提供日期间隔。但是,您可以使用 Streamin API 将最近发布的推文捕获为包含您提供的关键字的推文流。
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);
}
回答by crjn
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");
}
}