I'm new to Python, and I'm attempting to save data from the streaming Twitter API to a CSV file. I can successfully print content to my console, but I cannot get it to save.
I've done a search on stack and I've found several examples which come very close to answering my question, but none which I've found very adaptable due to my very limited skillset.
My code to print to console is as follows:
import sys
import tweepy
#pass security information to variables
consumer_key=""
consumer_secret=""
access_key = ""
access_secret = ""
#use variables to access twitter
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
#create an object called 'customStreamListener'
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
print status.author.screen_name, status.created_at, status.text
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True # Don't kill the stream
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
streamingAPI = tweepy.streaming.Stream(auth, CustomStreamListener())
streamingAPI.filter(track=['russia'])