Using tweepy to access Twitter's Streaming API

荒凉一梦 提交于 2019-12-20 09:45:44

问题


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.

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

  1. O'Rilley Answers code: How to Capture Tweets in Real-time with Twitter's Streaming API

  2. Andrew Robinson's blog: Using Tweepy to access the Twitter Stream

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

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.

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)

What am I doing wrong?


回答1:


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():

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/173 https://github.com/tweepy/tweepy/pull/182



来源:https://stackoverflow.com/questions/10970550/using-tweepy-to-access-twitters-streaming-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!