NameError: name 'MyStreamListener' is not defined

0

I'd import 'tweepy' package for accessing twitter data for analysis and visualization in Python. Afterwards I did setup oauth authentication and everything else but on the next step, which is STREAMING TWEETS for me am using MyStreamListener. This causing me a Name Error--

NameError: name 'MyStreamListener' is not defined

Here's my code-

# API Authentication 

# Importing TWEEPY Library
import tweepy

# Store OAuth authentication credentials in relevant variables
access_token = "provided"
access_token_secret = "provided"
consumer_key = "provided"
consumer_secret = "provided"

# Pass OAuth details to tweepy's OAuth handler
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

# Streaming Tweets  

# Initialize Stream listener
l = MyStreamListener()

# Create you Stream object with authentication
stream = tweepy.Stream(auth, l)

# Filter Twitter Streams to capture data by the keywords:
stream.filter(track = ['clinton', 'trump', 'sanders', 'cruz'])

What should I do now? Any thoughts? Cheers!

Abhishek Jaiswal

Posted 2016-10-15T13:25:37.450

Reputation: 1 719

Answers

1

You need to override StreamListener() class.

# API Authentication 

# Importing TWEEPY Library
import tweepy

# Store OAuth authentication credentials in relevant variables
access_token = "provided"
access_token_secret = "provided"
consumer_key = "provided"
consumer_secret = "provided"

# Pass OAuth details to tweepy's OAuth handler
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

# Streaming Tweets
#override tweepy.StreamListener to add logic to on_status
class MyStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print(status.text)

# Initialize Stream listener
l = MyStreamListener()

# Create you Stream object with authentication
stream = tweepy.Stream(auth, l)

# Filter Twitter Streams to capture data by the keywords:
stream.filter(track = ['clinton', 'trump', 'sanders', 'cruz'])

manohar amrutkar

Posted 2016-10-15T13:25:37.450

Reputation: 121

Okay but, I think it will cause a connection error further or anything alike when I'll try to load the data? Any thoughts on that? – Abhishek Jaiswal – 2016-10-15T14:20:22.737

Twitter api has certain limits which you can check here Based on those limits you should make calls.

– manohar amrutkar – 2016-10-15T14:52:23.247

Oh great, let me give it a try. Hope it will work as desired. Thanks! – Abhishek Jaiswal – 2016-10-15T15:14:47.917

So did it work? – Emre – 2017-02-12T19:01:05.473