我尝试将API测试到breezometer.com.
当我将API密钥输入网络时 - 页面https://breezometer.com/api/
它返回预期的JSON回复。
但是,输入完全相同的数据 - 在下面的python脚本中模仿Web请求(来自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/
个证书(所有可用格式) - 遵循https://stackoverflow.com/a/31627786/1656850建议:仍然,我收到了CERTIFICATE_VERIFY_FAILED错误。< / p>
有关如何调试此问题的任何建议吗?
答案 0 :(得分:0)
使用Python 2.7添加verify = False,即
requests.get(url, headers=user_agent, verify=False)