0
$\begingroup$

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!

$\endgroup$

1 Answer 1

1
$\begingroup$

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'])
$\endgroup$
5
  • $\begingroup$ 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? $\endgroup$
    – Abhishek
    Oct 15, 2016 at 14:20
  • $\begingroup$ Twitter api has certain limits which you can check here Based on those limits you should make calls. $\endgroup$ Oct 15, 2016 at 14:52
  • $\begingroup$ Oh great, let me give it a try. Hope it will work as desired. Thanks! $\endgroup$
    – Abhishek
    Oct 15, 2016 at 15:14
  • $\begingroup$ So did it work? $\endgroup$
    – Emre
    Feb 12, 2017 at 19:01
  • $\begingroup$ No. It didn't but I tried some other approach and it worked like a magic. $\endgroup$
    – Abhishek
    Oct 25, 2022 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.