AttributeError:'OAuthHandler'对象没有属性'on_exception'

我收到的错误是:

AttributeError: 'OAuthHandler' object has no attribute 'on_exception'

,当我尝试为Twitter流API编译我的Python代码时 . 但我甚至没有使用 on_exception 方法,所以我不确定它是什么意思 .

from tweepy import Stream
from tweepy.auth import OAuthHandler
#import StreamListener from tweepy.streaming
from tweepy.streaming import StreamListener

#import the API() method for the intitilization method ___init__
from tweepy.api import API

import time
import simplejson
import tweepy

#define the OAuth variables

consumerKey = "XX"
consumerSecret = "XX"
accessTokenKey = "XX"
accessTokenSecret = "XX"

#now we need to log into the Twitter API using my credentials. This is practically aunthenticating my access to my Twitter API
key = tweepy.OAuthHandler(consumerKey, consumerSecret)
key.set_access_token(accessTokenKey, accessTokenSecret)

#to confirm thats its me I can ask for my application name and print it out
api = tweepy.API(key)
print api.me().name

#the StreamListener class literally listens for the tweets that We are looking for depending on what we set the track parameter to be
#create a new class for StreamListener() so as to modify this StreamListener class to be able to store the tweets we'll be streaming in as well as to stop reading tweets.
#the StreamListener class is from the tweepy module so invoke it using tweepy
#Listener() takes StreamListener() as its argument and thereby inherits everything that is in StreamListener()

class Listener(tweepy.StreamListener):

#the initialization function __init__()  initializes an instance of a class
#The second is the on_status() function. In Twitter, a tweet is known technically as a status update. on_status() tells StreamListener() what to do when it receives a new status update.

    def __init__(self, api=None):
        self.api = api or API()
#create a counter that increments itself by one every time that StreamListener() finds a relevant tweet,
        self.n = 0          #this is the counter which is initially 0
        self.m = 20         #in this example, we want 20 to be the max number of tweets at first so we can analyze them


    def on_status(self, status):
        print status.text.encode('utf8')
        self.n = self.n + 1           #after incrememnting the counter, we want to checkk if we have reached the maximum yet hence we use the if statement
                #If it is, on_status() returns True, which tells Listener() to keep listening for more tweets. Otherwise, on_status() returns False, which tells Listener() to stop listening and ultimately turn tweepy off.
        if self.n < self.m:
            return True
        else:
            return False

#now to call the StreamListener class
#assign the output of Listener() to a variable, using your login credentials loaded above into key and the Listener()
stream = tweepy.streaming.Stream(Listener, key)

#define the stream by giving a parameter to listen for: a keyword – a list of strings assigned to track , in this case 1,

stream.filter(track=['de'])

#if we wanted to listen for a specific language, we'd also list that parameter as languages=['en'] for English

我从终端得到的错误说:

回溯(最近一次调用最后一次):文件“twitterAPI.py”,第63行,在stream.filter中(track = ['de'])文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2 .7 / site-packages / tweepy / streaming.py“,第430行,在过滤器self._start(async)文件中”/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ tweepy / streaming.py“,第346行,在_start self._run()文件”/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tweepy/streaming.py“,行285,在_run self.listener.on_exception(exception)AttributeError:'OAuthHandler'对象没有属性'on_exception'

回答(0)