Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.1K
Views
No se puede importar StreamListener

Estoy tratando de crear un flujo de datos en Python usando la API de Twitter, pero no puedo importar StreamListener correctamente.

Aquí está mi código:

 import tweepy from tweepy import Stream from tweepy.streaming import StreamListener class MyListener(StreamListener): def on_data(self, data): try: with open('python.json', 'a') as f: f.write(data) return True except BaseException as e: print("Error on_data: %s" % str(e)) return True def on_error(self, status): print(status) return True twitter_stream = Stream(auth, MyListener()) twitter_stream.filter(track=['#python'])

Y estoy recibiendo este error:

 Traceback (most recent call last): File "c:\Users\User\Documents\GitHub\tempCodeRunnerFile.python", line 6, in <module> from tweepy.streaming import StreamListener ImportError: cannot import name 'StreamListener' from 'tweepy.streaming' (C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\tweepy\streaming.py)
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Tweepy v4.0.0 se lanzó ayer y fusionó StreamListener en Stream .

Recomiendo actualizar su código a la subclase Stream en su lugar.
Alternativamente, puede cambiar a la versión 3.10.0.

over 4 years ago · Santiago Trujillo Report

0

Para Tweepy v4 , no necesita usar StreamListener . El siguiente es un código editado para v4:

 import tweepy class MyListener(tweepy.Stream): def on_data(self, data): try: with open('python.json', 'a') as f: f.write(data) return True except BaseException as e: print("Error on_data: %s" % str(e)) return True def on_error(self, status): print(status) return True twitter_stream = MyListener( "Consumer Key here", "Consumer Secret here", "Access Token here", "Access Token Secret here" ) twitter_stream.filter(track=['#python'])
over 4 years ago · Santiago Trujillo Report

0

Un ligero ajuste arreglará esto

 #from tweepy.streaming import StreamListener #OLD CODE import tweepy # NEW CODE #class TweetListener (StreamListener): #OLD CODE class TweetListener (tweepy.Stream): # NEW CODE
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!