【问题标题】:iterate through a Twitter stream using tweepy使用 tweepy 遍历 Twitter 流
【发布时间】:2015-01-12 08:53:55
【问题描述】:

我有这段代码可以在 tweepy 中创建一个 Stream(我的目标是获取一个人的所有提及的流):

class StreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print(status.text)
    def on_error(self, status_code):
        if status_code == 420:
        #returning False in on_data disconnects the stream
        return False

l = StreamListener()
stream = tweepy.Stream(auth = api.auth, listener=l)

我想知道如何将每个提及与 tweepy.Stream 对象一起使用? 我尝试过类似的方法:

for mention in tweepy.Cursor(stream.filter(track=['@twitter'])).items():
    *code*

但这只会创建一个我无法操作或使用的流。我对可以使用任何提及该人的方式感兴趣? 提前致谢。

【问题讨论】:

    标签: python tweepy


    【解决方案1】:

    我发现只需将从流中获取的数据转换为 json 就可以让我使用数据!

    class StreamListener(tweepy.StreamListener):
        def on_data(self, data):
            data = json.loads(data)
            print(data.text)
        def on_error(self, status_code):
            if status_code == 420:
            #returning False in on_data disconnects the stream
            return False
    

    【讨论】:

      猜你喜欢
      • 2015-12-31
      • 2021-03-13
      • 2015-03-06
      • 1970-01-01
      • 2015-01-22
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 2021-10-18
      相关资源
      最近更新 更多