使用Python抓取Twitter位置时接收ReadTimeOut错误
我正在使用python运行以下代码,以获取特定边界框的twitter位置:使用Python抓取Twitter位置时接收ReadTimeOut错误,python,twitter,error-handling,timeout,tweepy,Python,Twitter,Error Handling,Timeout,Tweepy,我正在使用python运行以下代码,以获取特定边界框的twitter位置: 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 = '' fil
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])
这很有效,我得到了坐标。但是,每次程序停止后,我都会收到一个读取超时错误,如下所示:
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.
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
进口城市糖果
文件“UrbanTweets.py”,第52行,在
twitterStream.filter(位置=[10.01,46.85,13.09,49.43])
文件“C:\Python27\lib\site packages\tweepy\streaming.py”,第445行,在过滤器中
自启动(异步)
文件“C:\Python27\lib\site packages\tweepy\streaming.py”,第361行,在\u start中
self._run()
文件“C:\Python27\lib\site packages\tweepy\streaming.py”,第294行,正在运行
引发异常
ReadTimeoutError:HTTPSConnectionPool(host='stream.twitter.com',port=443):读取超时。
有人知道如何解决这个问题吗
非常感谢 我测试了您的代码,它运行良好(我在OSX、Python2.7上测试过),因此无法重现您的错误。也许你只是面对网络连接问题?您需要等待多长时间才能获得此错误 您可以在下面的示例中添加try-catch异常块:
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
如果它只发生在一些示例中,您可以捕获异常,然后决定稍后要做什么(执行重新请求),或者如果根本不需要,则忽略它。谢谢您的评论。我怎样才能在代码中捕捉到异常或忽略它呢!你说得对,互联网连接似乎真的是个问题。我现在在一个不同的网络中运行代码,并没有收到这样的错误。如果答案是正确的或有助于解决您的问题,请考虑将其标记为接受。