【问题标题】:"AttributeError: 'MyStreamListener' object has no attribute 'api'" tweepy error“AttributeError: 'MyStreamListener' 对象没有属性 'api'” tweepy 错误
【发布时间】:2018-07-12 17:55:56
【问题描述】:

我有一个 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'])

【问题讨论】:

    标签: python tweepy


    【解决方案1】:

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

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

    【讨论】:

    • 这也是我的问题
    猜你喜欢
    • 2021-11-25
    • 2021-03-18
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2018-03-15
    • 2021-05-08
    相关资源
    最近更新 更多