如何将流推文保存在 json 文件中
我想分析推文,并且我直接流式传输推文...我想流式传输并保存 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论