2

screen shot

hi, I am trying to run this python code for twitter scraping. as it is shown in the screenshot, it gives me a syntax error that I can't figure out. here is the whole code:

print ('Filtering the public timeline for track="%s"' % (q,))

twitter_stream = twitter.TwitterStream(auth=twitter_api.auth)

stream = twitter_stream.statuses.filter(track=q)

for tweet in stream:
    try:
        if tweet['truncated']:
            tweet_text = tweet['extended_tweet']['full_text']
        else:
            tweet_text = tweet['text']
        # write the values to file
        csvwriter.writerow([
            tweet['created_at'],
            getVal(tweet['user']['screen_name']),
            getVal(tweet_text),
            getVal(tweet['user']['location']),
            tweet['user']['statuses_count'],
            tweet['user']['followers_count'],
            tweet['user']['friends_count'],
            tweet['user']['created_at']
            ])
        # print something to the screen, mostly so we can see what is going on...
        print (tweet['user']['screen_name'].encode('utf-8'), tweet['text'].encode('utf-8'))
    except Exception, err:
        print err
        pass

1 Answers1

1

I can only assume you are running this using Python 3 and not 2.

This question explains that the format you've used for the except statement would be wrong in that case.

The TL;DR of that post is:

switch the comma to as: except Exception as err:

PyPingu
  • 1,697
  • 1
  • 8
  • 21