Geopy:捕获超时错误
我正在使用geopy对一些地址进行地理编码,我想捕获超时错误并打印出来,以便可以对输入进行质量控制。我正在将地址解析请求放入try / catch中,但无法正常工作。关于我需要做什么的任何想法?
这是我的代码:
try: location = geolocator.geocode(my_address)
except ValueError as error_message:
print("Error: geocode failed on input %s with message %s"%(a, error_message))
我得到以下异常:
File "/usr/local/lib/python2.7/site-packages/geopy/geocoders/base.py", line 158, in _call_geocoder raise GeocoderTimedOut('Service timed out')
geopy.exc.GeocoderTimedOut: Service timed out
先感谢
回答:
尝试这个:
from geopy.geocoders import Nominatimfrom geopy.exc import GeocoderTimedOut
my_address = '1600 Pennsylvania Avenue NW Washington, DC 20500'
geolocator = Nominatim()
try:
location = geolocator.geocode(my_address)
print(location.latitude, location.longitude)
except GeocoderTimedOut as e:
print("Error: geocode failed on input %s with message %s"%(my_address, e.message))
你还可以考虑增加对地理定位器的地理编码调用的超时时间。在我的示例中,它将类似于:
location = geolocator.geocode(my_address, timeout=10)
要么
location = geolocator.geocode(my_address, timeout=None)
以上是 Geopy:捕获超时错误 的全部内容, 来源链接: utcz.com/qa/429892.html