【问题标题】:Using tweepy to access Twitter's Streaming API使用 tweepy 访问 Twitter 的 Streaming API
【发布时间】:2012-06-13 19:03:33
【问题描述】:

我目前无法获取使用 tweepy 访问 Twitter 的 Streaming API 以正确运行的示例代码(错误...或者至少我期望它运行的方式)。我正在使用来自 GitHub(标记为 1.9 版)和 Python 2.7.1 的最新克隆 tweepy。

我尝试了来自三个来源的示例代码,在每种情况下都使用“twitter”作为跟踪的测试术语:

  1. O'Rilley 答案代码:How to Capture Tweets in Real-time with Twitter's Streaming API

  2. 安德鲁·罗宾逊的博客:Using Tweepy to access the Twitter Stream

  3. GitHub 上的 Tweepy 示例存储库(正如 Andrew Robinson 所做的那样,可以轻松修改以支持 OAuth 身份验证):streamwatcher.py

在这三种情况下,我得到相同的结果:身份验证成功,没有产生错误,并且主程序循环似乎正在执行而没有任何问题。我看到网络使用率跃升至约 200KB/s,python 进程跃升至接近 100% 的 CPU 使用率,所以我认为正在接收数据。但是,没有任何内容输出到控制台。

我怀疑 tweepy 的 Stream 类由于某种原因没有调用自定义回调方法。我已经尝试重写每个示例中的回调方法以生成输出 无论何时 他们被调用,这似乎证实了这一点。这是基于 Andrew Robinson 的博客条目的一段非常简单的测试代码(当然,我的应用程序的密钥已被移除):

# -*- coding: utf-8 -*-

import tweepy

consumer_key = ''
consumer_secret = ''

access_token_key = ''
access_token_secret = ''

auth1 = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth1.set_access_token(access_token_key, access_token_secret)

class StreamListener(tweepy.StreamListener):
    def on_status(self, tweet):
        print 'Ran on_status'

    def on_error(self, status_code):
        print 'Error: ' + repr(status_code)
        return False

    def on_data(self, data):
        print 'Ok, this is actually running'


l = StreamListener()
streamer = tweepy.Stream(auth=auth1, listener=l)
#setTerms = ['hello', 'goodbye', 'goodnight', 'good morning']
setTerms = ['twitter']
streamer.filter(track = setTerms)

我做错了什么?

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    我也遇到了这个问题,并通过将streaming.py中的第160行更改为

    来修复它
    if delimited_string.strip().isdigit():
    

    这似乎是 Tweepy 中的一个已知问题/错误 - 在进行所有调试之前应该检查问题列表 :) -

    https://github.com/tweepy/tweepy/pull/173 https://github.com/tweepy/tweepy/pull/182

    【讨论】:

    • 谢谢!!!像魅力一样工作。另外,感谢您在 GitHub 上查看问题列表的建议——这可能是我真正需要知道的;-)
    猜你喜欢
    • 2018-10-20
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    • 2016-01-22
    • 2017-02-07
    • 2016-11-08
    • 2018-06-27
    • 1970-01-01
    相关资源
    最近更新 更多