How to save a tweepy Twitter stream to a file?

前端 未结 2 685
盖世英雄少女心
盖世英雄少女心 2020-12-31 20:21

I have a working script that successfully gathers tweets that mention \"stackoverflow\". However, I want to run the script in iPython (rather than executive a separate .py f

2条回答
  •  独厮守ぢ
    2020-12-31 20:47

    add the following code to your existing code. 'fetched_tweets.txt' is the name of file in which you want to save the tweets which is opened in 'a'(append mode).

    class StdOutListener(StreamListener):
    
        def on_data(self, data):
            #print data
            with open('fetched_tweets.txt','a') as tf:
                tf.write(data)
            return True
    
        def on_error(self, status):
            print status
    

提交回复
热议问题