ich den folgenden Code mit Python läuft twitter Stellen für eine bestimmte Begrenzungsrahmen zu greifen:Readtimeout Fehler empfangen, wenn Grabbing
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])
Dieses gut funktioniert und ich bin immer Koordinaten. Doch jedes Mal nach einer Weile stoppt das Programm und ich eine Lesezeit heraus Fehler erhalten, die wie folgt aussieht:
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.
Hat jemand eine Idee, wie dieses Problem zu lösen?
vielen dank!
Wenn es nur bei einigen Proben geschieht, können Sie die Ausnahme abfangen und später entscheiden, was zu tun ist (eine erneute Suche tun) oder wenn überhaupt nicht benötigt, ignorieren Sie es einfach. – user1767754
danke für deinen Kommentar. Wie kann ich die Ausnahme abfangen oder technisch innerhalb des Codes ignorieren? – Sebastian