【问题标题】:Is it normal that sampling tweets using TwitterStream as in Twitter4J code example, I get just mainly question marks as user name and status?在 Twitter4J 代码示例中使用 TwitterStream 对推文进行采样是否正常,我主要得到问号作为用户名和状态?
【发布时间】:2016-04-22 03:34:37
【问题描述】:

我使用了 Twitter4j 中“代码示例”部分中的代码:

public static void main(String[] args) throws TwitterException, IOException{
    StatusListener listener = new StatusListener(){
        public void onStatus(Status status) {
            System.out.println(status.getUser().getName() + " : " + status.getText());
        }
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    };
    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(listener);
    // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
    twitterStream.sample();
}

如您所见,上面的代码中有一个 println,位于方法“onStatus”中。下图显示了我主要从该代码中获得的信息。正常吗?

question marks...question marks everywhere

确实,我只过滤用户名中没有问号的状态,我几乎什么也没得到。此外,我还应该过滤位置公开的用户。关于这一点,我还问有什么区别:

user.isGeoEnabled()

user.getLocation() != ""

【问题讨论】:

    标签: java twitter twitter4j twitter-streaming-api


    【解决方案1】:

    您将得到的回复是 UTF-8 编码的https://dev.twitter.com/tags/utf-8

    如果您查看输出中的一些帐户,它们包含非西欧字符https://twitter.com/tomokichi_koyo。这些正在破坏输出。

    尝试改为写入文件并使用支持 UTF-8 的编辑器打开。关于将 java 和您的操作系统设置为默认为 UTF-8 有多种答案,但您需要寻找您的特定组合 https://stackoverflow.com/search?q=windows+console+java+utf-8

    【讨论】:

    • 非常感谢您的建议。 @Yuri Schmike 我现在已经解决了这个问题,只对英文推文进行采样(使用“sample("en")"),但我认为当我有更多时间时我会采用更聪明的策略。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 2014-12-14
    • 1970-01-01
    • 2019-01-24
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多