Search code examples
pythontwittererror-handlingtimeouttweepy

Receiving ReadTimeOut Error when grabbing Twitter locations with Python


I am running the following code with python to grab twitter locations for a specific bounding box:

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

#Enter Twitter API Key information
consumer_key = ''
consumer_secret = ''
access_secret = ''

file = open("C:\Python27\Output2.csv", "w")
file.write("X,Y\n")

data_list = []
count = 0

class listener(StreamListener):

    def on_data(self, data):
        global count

        #How many tweets you want to find, could change to time based
        if count >= 0:
            json_data = json.loads(data)

            coords = json_data["coordinates"]
            if coords is not None:
               print coords["coordinates"]
               lon = coords["coordinates"][0]
               lat = coords["coordinates"][1]

               data_list.append(json_data)

               file.write(str(lon) + ",")
               file.write(str(lat) + "\n")

               count += 1
            return True
        else:
            file.close()
            return False

    def on_error(self, status):
        print status

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
twitterStream = Stream(auth, listener())
#What you want to search for here
twitterStream.filter(locations=[10.01,46.85,13.09,49.43])

This works well and I am getting coordinates. However, everytime after a while the program stops and I receive a read time out error which looks like this:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import UrbanTweets
  File "UrbanTweets.py", line 52, in <module>
    twitterStream.filter(locations=[10.01,46.85,13.09,49.43])
  File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 445, in filter
    self._start(async)
  File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 361, in _start
    self._run()
  File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 294, in _run
    raise exception
ReadTimeoutError: HTTPSConnectionPool(host='stream.twitter.com', port=443): Read timed out.

Does anyone have an idea how to solve this issue?

many thanks!


Solution

  • I tested your code and it works fine (I tested it on OSX, Python 2.7.) so I could not reproduce your error. Maybe you simply face internet connection problems? How long do you have to wait to get this error?

    You can add try-catch exception block in example here:

    while True:
            try:
                    auth = OAuthHandler(consumer_key, consumer_secret)
                    auth.set_access_token(access_token, access_secret)
                    twitterStream = Stream(auth, listener())
                    #What you want to search for here
                    twitterStream.filter(locations=[10.01,46.85,13.09,49.43])
            except Exception as e:
                    #Handle expception here (print, pass, break..?)
                    print e
                    pass