treetop treetop - 1 year ago 72
JSON Question

Python Tweepy occasional exception during streaming

I'm using this code to capture the content of live streams using the 'text' identifier which basically captures the actual tweet text from Tweepys streaming capture function. It seems to be working for the most part, but I also receive the occasional "Not Working" message as a result of the exception in the code when I run the script (it happen maybe once every 10 10 seconds or so when I search a term that is trending heavily). The output file still capture many tweets despite this. I was wondering if anyone knew what could be causing this exception, as I'm worried I may be missing the occasional tweet as a result of it.

class MyListener(StreamListener):

def on_data(self, data):
with open('python.json', 'a') as f:
datadict = json.loads(data)
return True
except BaseException as e:
print("Not Working")
return True

def on_error(self, status):
return True

twitter_stream = Stream(auth, MyListener())

Answer Source

You might have accessed a tweet that is private. You can check the exception by trying this:

except tweepy.TweepError as e:
    print e
    print type(e)
    print e.__dict__
    print e.reason
    print type(e.reason)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download