harreskhan harreskhan - 2 months ago 51
Python Question

Python Twitter API Stream tweepy trying to save data to a CSV file

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

ckey = 'hidden due to question'
csecret = 'hidden due to question'
atoken = 'hidden due to question'
asecret = 'hidden due to question'

class listener(StreamListener):

def on_data(self, data):
try:
print (data)
saveFile = open('TwitterAPI.csv','a')
saveFile.write(data)
saveFile.Write('\n')
saveFIle.close()
return (True)
def on_error(self, status):
print (status)

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["car"])


The problem with the code above is the fact that it keeps on coming up with an error message stating that there is an unexpected unindent at the //def on_error// row

Answer

You open a try block without catching the exception.

https://docs.python.org/3/tutorial/errors.html

Also be careful, python is case sensitive, so saveFile is not saveFIle, nor saveFile.write() is saveFile.Write()...

Editing your on_data() handler as follow should make it work :

def on_data(self, data):
    try:
        print(data)
        with open('TwitterAPI.csv','a') as f:
            f.write(data)
    except Exception as e: # here catch whatever exception you may have.
        print('[!] Error : %s' % e)

Edit : Below is your full code reworked :

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

ckey = 'hidden due to question'
csecret = 'hidden due to question'
atoken = 'hidden due to question'
asecret = 'hidden due to question'


class listener(StreamListener):

    def on_data(self, data):
        try:
            print(data)
            with open('TwitterAPI.csv','a') as f:
                f.write(data)
        except:
            pass

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

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["car"])