如何从 tweepy 流中获取单个项目

How to get individual items from tweepy stream

我似乎无法获取单个项目,即仅获取文本或仅获取用户。

我试过将其用作字典或列表,但似乎没有任何效果。但是,我不知道我的测试有多可信,因为我是一个业余爱好者。

def on_data(self, data):
        try:
            print(data.text(?))
            with open(self.fetched_tweets_filename, 'a') as tf:
                tf.write(data)
            return True
        except BaseException as e:
            print("Error on_data %s" % str(e))
        return True

我想要 "Sample tweet" 但是我遇到了各种无法解析的错误

StreamListener 的 on_data 方法接收字符串形式的原始数据。
您可以使用 json.loads 将 JSON 反序列化为字典以访问 "text" 键。
参见 https://github.com/tweepy/tweepy/blob/29bae1fde3ae0636fa13e5ed69f68f2af75c110b/tweepy/streaming.py#L42-L48

或者,您可以使用 on_status 来获取具有 text 属性的 Status 对象。