如何使用Nominatim处理错误?

-1 投票
1 回答
592 浏览
提问于 2025-04-19 05:11

我正在使用geopy运行一个Nominatim网络服务,但它经常失败,可能是因为使用政策或者网络连接的问题。我该怎么处理这种连接失败的情况呢?我想在失败后停止一段时间,然后再重新运行代码。错误信息是:

GeocoderServiceError: <urlopen error [Errno 10060] A connection attempt failed because the
connected party did not properly respond after a period of time, or established connection 
failed because connected host has failed to respond>

还有

GeocoderServiceError: HTTP Error 420: unused

伪代码大概是这样的:

try:
    run web service
except:
    stop several seconds or minutes and rerun webservice at the same line and loops
(if it fails again)
    stop 30 minutes and rerun webservice

任何提示或建议都非常欢迎。

谢谢!

1 个回答

1

感谢大家的评论。修改一下try/except的用法就是解决办法。

根据geopy的文档(http://geopy.readthedocs.org/en/latest/#exceptions),使用geopy时最常见的错误是GeocoderServiceError。下面是修改后的代码,用来处理这些错误。

try:
    run web service
except geopy.exc.GeocoderServiceError as e:
    if e.message == 'HTTP Error 420: unused':
        time.sleep(1800)
        run web service
    elif e.message == '<urlopen error [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>':
        time.sleep(5)
        run web service

撰写回答