Geopy:捕获超时错误 [英] Geopy: catch timeout error

查看:123
本文介绍了Geopy:捕获超时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用geopy对一些地址进行地理编码,我想捕获超时错误并打印出来,以便可以对输入进行质量控制.我将地理编码请求放入了try/catch中,但是没有用.关于我需要做什么的任何想法?

I am using geopy to geocode some addresses and I want to catch the timeout errors and print them out so I can do some quality control on the input. I am putting the geocode request in a try/catch but it's not working. Any ideas on what I need to do?

这是我的代码:

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 Nominatim
from 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))

您还可以考虑增加对地理定位器的地理编码调用的超时时间.在我的示例中,它将类似于:

You can also consider increasing the timeout on the geocode call you are making to your geolocator. In my example it would be something like:

location = geolocator.geocode(my_address, timeout=10)

location = geolocator.geocode(my_address, timeout=None)

这篇关于Geopy:捕获超时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆