Python Geopy:捕获超时错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27914648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:27:41  来源:igfitidea点击:

Geopy: catch timeout error

pythonscrapygeopy

提问by MoreScratch

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?

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

Here is my code:

这是我的代码:

try:
  location = geolocator.geocode(my_address)               
except ValueError as error_message:
  print("Error: geocode failed on input %s with message %s"%(a, error_message))

I get the following exception:

我收到以下异常:

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

Thank you in advance!

先感谢您!

采纳答案by Imran

Try this:

尝试这个:

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)

or

或者

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

回答by tylerjw

You may be experiencing this problem because you tried to request this address multiple times and they temporarily blocked you or slowed you down because of their 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.

您遇到此问题的原因可能是您多次尝试请求此地址,但由于他们的使用政策,他们暂时阻止了您或减慢了您的速度。它声明每秒不超过一个请求,并且您应该缓存您的结果。我遇到了这个问题,你有几个解决方案。如果您不想对代码进行太多更改,您可以获得一个 Google API 密钥,您可以免费使用该密钥每天处理 2500 个请求,或者您可以缓存您的结果。因为我已经在 AWS 上使用 DynamoDB 解决我的问题,所以我继续创建了一个表来缓存我的结果。 这是我的代码的要点。

回答by sri

I dealt with the Same Problem for so many days this is my code:

我处理了同样的问题这么多天这是我的代码:

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

ERRORService timed out

ERROR服务超时

solution: Add a new attribute that declares the timeout:

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

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