如何将流推文保存在 json 文件中

发布于 2025-01-09 21:25:16 字数 1501 浏览 1 评论 0原文

我想分析推文,并且我直接流式传输推文...我想流式传输并保存 json 格式的推文。

如何在通过 twitter api 流式传输推文期间以 json 格式保存,我编写并获取流式推文,但我在流式传输时没有保存 json 文件。

class TwitteStreamer():

   def stream_tweet(self,hashtag_list):

      stream = tweepy.Stream(
      twitter_key.consumer_key, twitter_key.consumer_secret,
      twitter_key.access_token, twitter_key.access_token_secret
      )

    stream.filter(track=hashtag_list, languages=["en"])

这是自定义监听器

class TweetPrinter(tweepy.Stream):

   file = open('tweets.json', 'a')


   def on_status(self, status):
     print(status.text)
    
   def on_data(self,tweet):
     json_data = json.loads(data)
     file.write(str(json_data))
    
   def on_error(self, status_code):
     print(status_code)
     time.sleep(120)
     return False # To continue listening

   def on_timeout(self):
     time.sleep(120)
     return True # To continue listening



class Printer():

   def print_tweets(self, hashtag_list):

     printer = TweetPrinter(
      twitter_key.consumer_key, twitter_key.consumer_secret,
      twitter_key.access_token, twitter_key.access_token_secret
     )
     #printer.filter(track=hashtag_list)
     printer.filter(track=hashtag_list ,languages=["en"])



hashtag_list = ["btc","bitcoin","Bitcoin","#btc","#Bitcoin","BTC","#BTC"]

fetched_tweets_filename = "tweets.json"

twitter_streamer = TwitteStreamer()
twitter_streamer.stream_tweet(hashtag_list)

printer = Printer()
printer.print_tweets(hashtag_list)

I want to analyze tweets and ı got streamming tweets.... directly I want to streaming and saving json format of tweets.

how to save in json format while during streaming tweets via twitter api, I wrote and get stream tweets but ı didn't save json file while streaming.

class TwitteStreamer():

   def stream_tweet(self,hashtag_list):

      stream = tweepy.Stream(
      twitter_key.consumer_key, twitter_key.consumer_secret,
      twitter_key.access_token, twitter_key.access_token_secret
      )

    stream.filter(track=hashtag_list, languages=["en"])

this is custom listener

class TweetPrinter(tweepy.Stream):

   file = open('tweets.json', 'a')


   def on_status(self, status):
     print(status.text)
    
   def on_data(self,tweet):
     json_data = json.loads(data)
     file.write(str(json_data))
    
   def on_error(self, status_code):
     print(status_code)
     time.sleep(120)
     return False # To continue listening

   def on_timeout(self):
     time.sleep(120)
     return True # To continue listening



class Printer():

   def print_tweets(self, hashtag_list):

     printer = TweetPrinter(
      twitter_key.consumer_key, twitter_key.consumer_secret,
      twitter_key.access_token, twitter_key.access_token_secret
     )
     #printer.filter(track=hashtag_list)
     printer.filter(track=hashtag_list ,languages=["en"])



hashtag_list = ["btc","bitcoin","Bitcoin","#btc","#Bitcoin","BTC","#BTC"]

fetched_tweets_filename = "tweets.json"

twitter_streamer = TwitteStreamer()
twitter_streamer.stream_tweet(hashtag_list)

printer = Printer()
printer.print_tweets(hashtag_list)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文