attributeError:对象没有属性' user_agent'
我正在使用Twitter API(特别是Twitter API V2)编写一个Tweepy Bot。该脚本应该使用代码单词流式传输一些推文并将其写入文件。我正在尝试将文件名
参数添加到stdoutlistener
类的构造函数。
import tweepy
import credentials
class StdOutListener(tweepy.StreamingClient):
def __init__(self, bearer_token, filename):
self.bearer_token = bearer_token
self.filename = filename
def on_data(self, data):
with open(self.filename) as file:
file.write(data)
print("[+] File written!")
return True
def on_error(self, status):
print(status)
if __name__ == "__main__":
filename = "tweets.json"
listener = StdOutListener(credentials.BEARER_TOKEN, filename)
listener.add_rules(tweepy.StreamRule("Boston Celtics"))
listener.filter()
但是,每当我运行此操作时,都会收到错误:attributeError:'stdoutlistener'对象没有属性'user_agent'
。我认为这与继承有关。因此,我尝试使用super
,但无济于事。我已经在Stackoverflow上搜索以找到解决此问题的答案,但没有找到一个问题。是什么原因导致属性?
I'm writing a tweepy bot using the Twitter API, specifically the Twitter API v2. The script is supposed to stream some tweets using a code word and write them to a file. I'm trying to add a filename
parameter to the constructor of the StdOutListener
class.
import tweepy
import credentials
class StdOutListener(tweepy.StreamingClient):
def __init__(self, bearer_token, filename):
self.bearer_token = bearer_token
self.filename = filename
def on_data(self, data):
with open(self.filename) as file:
file.write(data)
print("[+] File written!")
return True
def on_error(self, status):
print(status)
if __name__ == "__main__":
filename = "tweets.json"
listener = StdOutListener(credentials.BEARER_TOKEN, filename)
listener.add_rules(tweepy.StreamRule("Boston Celtics"))
listener.filter()
However, whenever I run this I get the error: AttributeError: 'StdOutListener' object has no attribute 'user_agent'
. I think it has something to do with a inheritance. So I've tried using super
but to no avail. I've searched on StackOverflow to find an answer to this problem, but haven't found one. What's causing the AttributeError?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是遇到了这个问题,试图将参数传递到该类别。
您需要超级()。 init (berer_token)
Just had this issue trying to pass a parameter to that class.
You need to super().init(bearer_token)