" AttributeError:' MyStreamListener'对象没有属性' api'" tweepy错误

时间:2018-06-02 01:18:04

标签: python tweepy

我有一个python程序,可以在Twitter上搜索一个单词并计算该单词的所有提及。但是,我遇到了一个奇怪的问题,我无法在其他地方找到答案。我得到了一个" AttributeError:' MyStreamListener'对象没有属性' api'"错误。这是我第一次看到这个错误。有关如何修复的任何建议?

代码:

from tweepy import OAuthHandler

import tweepy
from tweepy import StreamListener
from tweepy import Stream


import time



consumer_key = 'super secret consumer key'
consumer_secret = 'Please help Ive been stuck with this error for days'
access_token = 'Im so desperate'
access_secret = 'I suck at coding please help'

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

api = tweepy.API(auth)
print('')


class MyStreamListener(tweepy.StreamListener):



    def __init__(self):
        #initializes the counter
        self.counter = 0    



    def on_status(self, status):
        #prints status text. Also counts the mentions. 
        self.counter = self.counter + 1
        print(status.text)


    def on_error(self, status_code):
        if status_code == 420:
            print('420 error')
            #Ends stream in case of rate limiting
            return False


myStreamListener = MyStreamListener()

myStream = tweepy.Stream(auth = api.auth, listener = myStreamListener)

#Word
myStream.filter(track=['Warriors'])

1 个答案:

答案 0 :(得分:3)

super(MyStreamListener, self).__init__()开头添加__init__可以为我解决。

def __init__(self):
    super(MyStreamListener, self).__init__()
    #initializes the counter
    self.counter = 0