首页 文章

如何找到根本原因:urllib2.URLError:<urlopen error [SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败(_ssl.c:590)?

提问于
浏览
2

我正在尝试测试API到 breezometer.com. 当我将我的API密钥输入到网页 https://breezometer.com/api/ 时,它会返回预期的JSON回复 .

但是,输入完全相同的数据 - 在以下模仿Web请求的python脚本中(来自Python 2和3):

$ cat test2.py
#!/usr/bin/env python

"""
Location from Google Maps: https://www.google.co.il/maps/place/Haifa+Port,+Haifa/@32.8267212,34.9852862,15z/data=!4m2!3m1!1s0x151dbbcd3a79ff47:0x8d20ff1e4833b549?hl=en

request: https://api.breezometer.com/baqi/?lat=32.8267212&lon=34.9852862&key=API_KEY
"""

from bs4 import BeautifulSoup
import urllib2

latitude = 32.8267212
longitude = 34.9852862
api_key = "XXXXXfb123e242839edeb10539dXXXXX"

url = "https://api.breezometer.com/baqi/?lat={0}&lon={1}&key={2}".format(latitude, longitude, api_key)

print(BeautifulSoup(urllib2.urlopen(url)))

我明白了:

$ python test2.py
Traceback (most recent call last):
  File "test2.py", line 18, in <module>
    print(BeautifulSoup(urllib2.urlopen(url)))
  File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 431, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 449, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1240, in https_open
    context=self._context)
  File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>

我看过类似的SO线程(例如Python 'requests' [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)来自https://stackoverflow.com/search?q=_ssl.c%3A590+CERTIFICATE_VERIFY_FAILED

我已经向Chrome导入 https://breezometer.com/api/https://breezometer.com/ certificats(所有可用格式) - 遵循https://stackoverflow.com/a/31627786/1656850建议:仍然,我收到了CERTIFICATE_VERIFY_FAILED错误 .

Any suggestions how to debug this issue?

1 回答

  • 0

    使用Python 2.7添加verify = False即

    requests.get(url, headers=user_agent, verify=False)
    

相关问题