I have a python program that searches twitter for a word and counts all mentions of that word. However, I've run into an odd problem and I can't find an answer elsewhere. I'm getting a "AttributeError: 'MyStreamListener' object has no attribute 'api'" error. This is the first time I've seen this error. Any suggestions on how to fix?
Code:
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'])
Adding super(MyStreamListener, self).__init__()
at the beginning of __init__
fixes it for me.
def __init__(self):
super(MyStreamListener, self).__init__()
#initializes the counter
self.counter = 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With