Java 获取公共 Twitter 个人资料的推文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2943161/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:44:50  来源:igfitidea点击:

Get tweets of a public twitter profile

javaapitwittertwitter4j

提问by Dexter

I have a list of usernames on Twitter whose profiles are public. I wish to get "all the tweets" they have posted from the day they formed their profile. I checked Twitter4J examples on GitHub.
According to the Twitter API documentation, only the 20 most recent tweets are returned. Is there anyway I could perform my task?

我在 Twitter 上有一个用户名列表,其个人资料是公开的。我希望获得他们从形成个人资料之日起发布的“所有推文”。我在GitHub 上查看了 Twitter4J 示例。
根据 Twitter API 文档,只返回最近的 20 条推文。无论如何我可以执行我的任务吗?

采纳答案by Tyler

To use Twitter4J to get all posts from a user you'll have to make your request over multiple pages..

要使用 Twitter4J 获取用户的所有帖子,您必须在多个页面上提出请求。

Below code based of an example on GitHub

以下代码基于GitHub 上的示例

Twitter unauthenticatedTwitter = new TwitterFactory().getInstance();
//First param of Paging() is the page number, second is the number per page (this is capped around 200 I think.
Paging paging = new Paging(1, 100);
List<Status> statuses = unauthenticatedTwitter.getUserTimeline("google",paging);

Just loop and keep grabbing new pages until there are no new posts should work.

只需循环并继续抓取新页面,直到没有新帖子应该工作。

回答by Chris McFarland

If you read through the Twitter's Documentation, you can retrieve up to 200 tweets at a time if you specify "count=200" in your API request.

如果您通读Twitter 的文档,如果您在 API 请求中指定“count=200”,则一次最多可以检索 200 条推文。

You can also use "page=x" to get different paginated results; you could keep doing this until you've retrieved every tweet the user has posted.

您也可以使用“page=x”来获得不同的分页结果;您可以继续这样做,直到您检索到用户发布的每条推文为止。

I'm not sure how your Java application would create this, but your requests would probably look like:

我不确定您的 Java 应用程序将如何创建它,但您的请求可能如下所示:

http://api.twitter.com/1/statuses/user_timeline.xml?id=SomeUsername&count=200&page=1
http://api.twitter.com/1/statuses/user_timeline.xml?id=SomeUsername&count=200&page=2
http://api.twitter.com/1/statuses/user_timeline.xml?id=SomeUsername&count=200&page=3

... etc.

... 等等。

Keep in mind that these requests are rate-limited so you'd need to be careful not to exceed the limit.

请记住,这些请求是有速率限制的,因此您需要小心不要超过限制。

回答by rednoyz

Here is how to get ALL tweets for a user (or at least up to ~3200):

以下是获取用户的所有推文(或至少约 3200 条)的方法:

import java.util.*;
import twitter4j.*;
import twitter4j.conf.*;

public static void main(String[] a) {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey("YOUR KEYS HERE");
    cb.setOAuthConsumerSecret("YOUR KEYS HERE");
    cb.setOAuthAccessToken("YOUR KEYS HERE");
    cb.setOAuthAccessTokenSecret("YOUR KEYS HERE");

    Twitter twitter = new TwitterFactory(cb.build()).getInstance();

    int pageno = 1;
    String user = "cnn";
    List statuses = new ArrayList();

    while (true) {

      try {

        int size = statuses.size(); 
        Paging page = new Paging(pageno++, 100);
        statuses.addAll(twitter.getUserTimeline(user, page));
        if (statuses.size() == size)
          break;
      }
      catch(TwitterException e) {

        e.printStackTrace();
      }
    }

    System.out.println("Total: "+statuses.size());
}