Geopy:捕获超时错误

Geopy: catch timeout error

我正在使用 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 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))

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

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

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

您可能会遇到此问题,因为您多次尝试请求此地址,但由于他们的 usage policy. It states no more requests than one per second and that you should cache your results. I ran into this problem and you have a couple solutions. If you don't want to change your code much you can get a Google API key that you can use for something like 2500 requests/day for free or you can cache your results. Because I was already using DynamoDB on AWS for my problem I went ahead and just created a table that I cache my results in. Here is the gist of my code.

,他们暂时阻止了您或减慢了您的速度

我处理同一个问题这么多天这是我的代码:

geolocator = Nominatim(user_agent="ny_explorer")
location = geolocator.geocode(address_venue)

错误 服务超时

解决方案:添加一个声明超时的新属性:

location = geolocator.geocode(address_venue,timeout=10000)