Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a tweepy Twitter stream to a file?

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 file). Ideally, I just want to open it ipyb file, select run all, and let it run for a week or so (not closing my laptop of course) and in result I have a .json file with a week's worth of tweets.

Here is what I have so far:

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener

access_token = "x"
access_token_secret = "x"
consumer_key = "x"
consumer_secret = "x"

# file name that you want to open is the second argument
save_file = open('data.json', 'a')

class listener(StreamListener):

    def on_data(self, data):
        print(data)
        return True

    def on_error(self, status):
        print(status)

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["stackoverflow"])
like image 792
Anton Avatar asked Jan 24 '15 20:01

Anton


2 Answers

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
like image 93
Ajeet Khan Avatar answered Nov 19 '22 10:11

Ajeet Khan


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 >.

like image 6
mooneral Avatar answered Nov 19 '22 10:11

mooneral