gpt4 book ai didi

Python urllib.urlopen 给出 [SSL : CERTIFICATE_VERIFY_FAILED] error

转载 作者:太空宇宙 更新时间:2023-11-03 14:42:26 25 4
gpt4 key购买 nike

我对 Python 比较陌生。尝试用 Python 编写一个函数,该函数将使用 Google Distance Matrix API 计算位置之间的距离。实际上,我过去曾成功使用过此代码,但现在出现以下错误,无法弄清楚发生了什么。

IOError: [Errno 套接字错误] [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败 (_ssl.c:590)

我知道问题不在于 URL,因为我可以使用我的 API key 在浏览器中输入它,它会为我提供我正在寻找的结果(即来自 Google Maps Distance Matrix API 的距离信息) .这似乎是 urllib.urlopen 函数调用的问题。我假设我以某种方式错误地调用了它,但我不知道如何调用它。有什么想法吗?

我使用的是 Python 2.7.10 版。

import urllib
import re

def getMiles(origin,destination):
#APIkey = "someAPIkey" #Update to prompt user for API key
query = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=" + origin + "&destinations=" + destination + "&key=" + APIkey
test = 0

fhand = urllib.urlopen(query) #CODE ERRORS OUT HERE
#Error handling to ensure query works
try:
for line in fhand:
line = str(line.strip())
#print line
if test == 1:
line = line.replace('"text" : "',"")
line = line.replace('",',"")
result = [origin,'"'+destination+'"'] + line.split()
break
if line.startswith('"distance"'):
test = 1
return result
except:
result = [origin,'"'+destination+'"',"ERROR","Query could not be opened"]
return result

最佳答案

这是对我有用的解决方案,在您的程序中添加以下代码,它应该可以修复您的错误,伙计。

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

关于Python urllib.urlopen 给出 [SSL : CERTIFICATE_VERIFY_FAILED] error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42048023/

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