python - 无法导入 StreamListener
问题描述
我正在尝试使用 Twitter API 在 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)
解决方案
Tweepy v4.0.0昨天发布并合并StreamListener
为Stream
.
我建议您将代码更新为子类Stream
。
或者,您可以降级到 v3.10.0。
推荐阅读
- azure - 可用于容器的 Azure 应用服务的秘密卷?
- algorithm - 为抽象队列数据结构创建潜在函数以显示恒定摊销时间复杂度
- ionic-framework - 无法使用 Ionic + IBM Cloud App ID 打开弹出窗口
- python - 非捕获组的正则表达式前瞻未按预期工作
- apache-kafka - Apache kafka 不使用 NIFI 推送消息
- javascript - Node.JS:在接收到标准输出数据后写入子进程生成
- python - WebRTC Streaming 不适用于广播 Gstreamer webrtcbin
- python - 使用 Python 将返回值从一个函数传递到另一个函数
- javascript - 使用 JavaScript/jquery 动态添加表行组成的下拉列表
- javascript - 将两位数添加到倒数计时器