我试图使用Twitter在Python中创建数据流,但无法正确导入StreamListener。
这是我的密码:
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'])我得到了一个错误:
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)发布于 2021-09-26 21:29:32
发布于 2022-02-02 11:10:36
稍微调整一下就能解决这个问题。
#from tweepy.streaming import StreamListener #OLD CODE
import tweepy # NEW CODE
#class TweetListener (StreamListener): #OLD CODE
class TweetListener (tweepy.Stream): # NEW CODEhttps://stackoverflow.com/questions/69338089
复制相似问题