How to save a tweepy Twitter stream to a file?

前端 未结 2 674
盖世英雄少女心
盖世英雄少女心 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
    
    0 讨论(0)
  • 2020-12-31 21:03

    You can do it by redirecting output to a file:

    in Terminal/CMD just type python twitter_streaming.py > twitter_data.txt

    for appending to an existing file use >> instead of >.

    0 讨论(0)
提交回复
热议问题