我想使用scala从特定用户中提取推文。我的代码如下:
import twitter4j._
import twitter4j.Status
import collection.JavaConversions._
import org.apache.spark.streaming.twitter._
import org.apache.spark.SparkConf
import org.apache.spark._
val CONSUMER_KEY = "hidden"
val CONSUMER_SECRET = "hidden"
val ACCESS_TOKEN = "hidden"
val ACCESS_TOKEN_SECRET = "hidden"
System.setProperty("twitter4j.oauth.consumerKey", CONSUMER_KEY)
System.setProperty("twitter4j.oauth.consumerSecret", CONSUMER_SECRET)
System.setProperty("twitter4j.oauth.accessToken", ACCESS_TOKEN)
System.setProperty("twitter4j.oauth.accessTokenSecret", ACCESS_TOKEN_SECRET)
val twitter = new TwitterFactory().getInstance
val statuses = twitter.search(new Query("aTwitterUser")).getTweets
statuses.foreach(status => println(status.getText + "\n"))
通过使用上面的代码我能够获得指定用户的所有推文以及用户的推文。这并没有解决我的问题。我只想打印用户发的推文。任何帮助将不胜感激。
问候!
答案 0 :(得分:1)
见http://twitter4j.org/en/code-examples.html
上的第4点使用'来源:'查询
java代码:
// The factory instance is re-useable and thread safe.
Twitter twitter = TwitterFactory.getSingleton();
Query query = new Query("source:twitter4j yusukey");
QueryResult result = twitter.search(query);
for (Status status : result.getTweets()) {
System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
}
更多查询运算符:https://dev.twitter.com/rest/public/search
特别关注'来自:'操作者:
Query query = new Query("from:twitter4j yusukey");