You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用geopy调用Nominatim时遭遇SSL证书验证失败问题求助

解决geopy调用Nominatim时的SSL证书验证错误

问题代码

from geopy.geocoders import Nominatim

gelocator = Nominatim(user_agent="autenticador")

full_address = 'AVENIDA EDSON RAMALHO, 122, 58030100, JOAO PESSOA'

location = geolocator.geocode(full_address)

location

错误信息

GeocoderUnavailable: HTTPSConnectionPool(host='nominatim.openstreetmap.org', port=443): Max retries exceeded with url: /search?q=AVENIDA+EDSON+RAMALHO%2C+122%2C+58030100%2C+JOAO+PESSOA&format=json&limit=1 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1129)')))

解决方案

1. 临时绕过SSL验证(仅测试环境)

这种方法跳过证书检查,适合快速测试,但禁止在生产环境使用:

import ssl
from geopy.geocoders import Nominatim

# 创建不验证证书的SSL上下文
ssl_ctx = ssl.create_default_context()
ssl_ctx.check_hostname = False
ssl_ctx.verify_mode = ssl.CERT_NONE

geolocator = Nominatim(user_agent="autenticador", ssl_context=ssl_ctx)

full_address = 'AVENIDA EDSON RAMALHO, 122, 58030100, JOAO PESSOA'
location = geolocator.geocode(full_address)
print(location)

2. 配置可信证书(生产环境推荐)

通过添加Nominatim的可信证书来解决验证问题:

  1. 访问https://nominatim.openstreetmap.org,在浏览器中导出站点的根证书或中间证书,保存为本地文件(比如nominatim_cert.pem)。
  2. 加载证书到SSL上下文:
import ssl
from geopy.geocoders import Nominatim

# 加载可信证书
ssl_ctx = ssl.create_default_context(cafile="nominatim_cert.pem")
geolocator = Nominatim(user_agent="autenticador", ssl_context=ssl_ctx)

full_address = 'AVENIDA EDSON RAMALHO, 122, 58030100, JOAO PESSOA'
location = geolocator.geocode(full_address)
print(location)

3. 排查网络代理/防火墙

如果你的网络使用了代理或防火墙,可能是代理插入了自签名证书导致验证失败。尝试:

  • 直接连接互联网,绕过代理测试
  • 将代理的自签名证书添加到系统或Python的可信证书列表中

内容的提问来源于stack exchange,提问作者Adilson Chrestani Junior

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 23:39:08