gpt4 book ai didi

python-3.x - 获取 [SSL : CERTIFICATE_VERIFY_FAILED] error when trying to run geocoder in geopy how can I verify the certificate to get needed output?

转载 作者:行者123 更新时间:2023-12-02 03:19:06 24 4
gpt4 key购买 nike

我正在学习 Python 的 Udemy 类(class),并且正在使用 pandas。目前,我正在使用 geopy 来尝试返回输入地址的坐标。特别是,我正在运行 ArcGIS 地理编码,但当我运行它时,我收到 SSL 错误,指出它无法获取 SSL 证书。我将在此处包含代码和错误:

这是我尝试运行的代码以及我应该得到的输出。下面是我实际得到的输出:

In [1]: from geopy.geocoders import ArcGIS
...: nom = ArcGIS()
...: nom.geocode("3995 23rd St, San Francisco, CA 94114")
Out[1]: Location(3995 23rd St, San Francisco, California, 94114, (37.75298458728149, -122.4317017142651, 0.0))

>>> import geopy
>>> from geopy import ArcGIS
>>> nom = ArcGIS()
>>> nom.geocode("3995 23rd St, San Francisco, CA 94114")

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1317, in do_open encode_chunked=req.has_header('Transfer-encoding')) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1229, in request self._send_request(method, url, body, headers, encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1275, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1224, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1016, in _send_output self.send(msg) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 956, in send self.connect() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1392, in connect server_hostname=server_hostname) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 412, in wrap_socket session=session File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 853, in _create self.do_handshake() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1117, in do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/geopy/geocoders/base.py", line 344, in _call_geocoder page = requester(req, timeout=timeout, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 525, in open response = self._open(req, data) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 543, in _open '_open', req) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain result = func(*args) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1360, in https_open context=self._context, check_hostname=self._check_hostname) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1319, in do_open raise URLError(err) urllib.error.URLError:

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/geopy/geocoders/arcgis.py", line 195, in geocode response = self._call_geocoder(url, timeout=timeout) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/geopy/geocoders/base.py", line 375, in _call_geocoder raise GeocoderServiceError(message) geopy.exc.GeocoderServiceError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)

最佳答案

好吧,我实际上找到了一个似乎适用于此的解决方案。以下代码使我能够更新和/或安装我的系统似乎没有或没有注意到的任何所需的 SSL 证书。我将附加代码以及我发现工作解决方案的堆栈和源代码所在的 Github:

# install_certifi.py
#
# sample script to install or update a set of default Root Certificates
# for the ssl module. Uses the certificates provided by the certifi package:
# https://pypi.org/project/certifi/
import os
import os.path
import ssl
import stat
import subprocess
import sys
STAT_0o775 = ( stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
| stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
| stat.S_IROTH | stat.S_IXOTH )
def main():
openssl_dir, openssl_cafile = os.path.split(
ssl.get_default_verify_paths().openssl_cafile)
print(" -- pip install --upgrade certifi")
subprocess.check_call([sys.executable,
"-E", "-s", "-m", "pip", "install", "--upgrade", "certifi"])
import certifi
# change working directory to the default SSL directory
os.chdir(openssl_dir)
relpath_to_certifi_cafile = os.path.relpath(certifi.where())
print(" -- removing any existing file or link")
try:
os.remove(openssl_cafile)
except FileNotFoundError:
pass
print(" -- creating symlink to certifi certificate bundle")
os.symlink(relpath_to_certifi_cafile, openssl_cafile)
print(" -- setting permissions")
os.chmod(openssl_cafile, STAT_0o775)
print(" -- update complete")
if __name__ == '__main__':
main()

Here是我找到解决方案的堆栈。 Here是源代码所在的 Github。

我亲自将这段代码复制到Sublime中并保存为.py文件,然后在python3中运行该程序。运行该程序后,我尝试使用 ArcGIS 重新运行我的 geopy 程序,现在已成功收到请求的坐标。

希望这对您有所帮助!

关于python-3.x - 获取 [SSL : CERTIFICATE_VERIFY_FAILED] error when trying to run geocoder in geopy how can I verify the certificate to get needed output?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55339873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com