How do I save streaming tweets in json via tweepy?

前端 未结 3 1046
醉话见心
醉话见心 2020-12-24 03:50

I\'ve been learning Python for a couple of months through online courses and would like to further my learning through a real world mini project.

For this project,

3条回答
  •  眼角桃花
    2020-12-24 04:49

    I found a way to save the tweets to a json file. Happy to hear how it can be improved!

    # initialize blank list to contain tweets
    tweets = []
    # file name that you want to open is the second argument
    save_file = open('9may.json', 'a')
    
    class CustomStreamListener(tweepy.StreamListener):
        def __init__(self, api):
            self.api = api
            super(tweepy.StreamListener, self).__init__()
    
            self.save_file = tweets
    
        def on_data(self, tweet):
            self.save_file.append(json.loads(tweet))
            print tweet
            save_file.write(str(tweet))
    

提交回复
热议问题