使用 tweepy 访问 Twitter 的 Streaming API [英] Using tweepy to access Twitter's Streaming API

查看:36
本文介绍了使用 tweepy 访问 Twitter 的 Streaming API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在获取使用 tweepy 访问 Twitter 的 Streaming API 以正确运行的示例代码时遇到了问题(错误……或者至少我希望它如何运行).我正在使用来自 GitHub(标记为 1.9 版)和 Python 2.7.1 的最新 tweepy 克隆.

I'm currently having trouble getting example code for using tweepy to access Twitter's Streaming API to run correctly (err...or at least how I expect it to run). I'm using a recent clone of tweepy from GitHub (labeled version 1.9) and Python 2.7.1.

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

I've tried example code from three sources, in each case using "twitter" as a test term for tracking:

  1. O'Rilley 回答代码:如何使用 Twitter 的流式 API 实时捕获推文

安德鲁罗宾逊的博客:使用 Tweepy 访问 Twitter 流

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

Tweepy examples repository on GitHub (which, as Andrew Robinson has done, can be easily modified to support OAuth authentication): streamwatcher.py

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

In all three cases I get the same result: Authentication is successful, no errors are produced, and the main program loop seems to be executing w/o any problems. I see network usage jump to about 200KB/s, and the python process jumps to near 100% CPU usage, so I think data is being received. Nothing is output to the console, however.

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

I suspect that tweepy's Stream class is not calling the custom callback method for some reason. I've tried rewriting the callback methods in each example to produce output whenever they're called, which seems to confirm this. This is one very simple bit of test code based on Andrew Robinson's blog entry (with my app's keys removed, of course):

# -*- 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)

我做错了什么?

推荐答案

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

I ran into this as well and fixed it on my local checkout by changing line 160 in streaming.py to

if delimited_string.strip().isdigit():

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

This seems to be a known issue/bug in Tweepy - should have checked the issues list before doing all that debugging :) -

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

这篇关于使用 tweepy 访问 Twitter 的 Streaming API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆