假设上面的“不能”不在你的代码中,如果可能的话,更新你的问题以省略“”,因为它会扰乱可读性或更改为其他占位符文本。
如错误所示,您尚未为计数器分配值,方法“on_status”将尝试递增计数器,但这只是方法的局部值,而不是对象的局部值,因此失败。
def on_status(self, status):
#prints status text. can be replaced with a counter probably.
counter = counter + 1
print(status.text)
您应该在
初始化
方法,然后使用self。相反,要反击。
添加
...
class MySteamListener(tweepy.StreamListener):
def __init__(self):
# Not sure if necessary, to make this work, but you could
# Initialize the inherited class as well (this may work only in Python 3)
# super().__init__()
self.counter = 0
...
将on_状态修改为
def on_status(self, status):
#prints status text. can be replaced with a counter probably.
self.counter = self.counter + 1
# Can be written as 'self.counter += 1'
print(status.text)