2015-12-24 31 views
2

我試圖測試API到breezometer.com.當我將我的API密鑰輸入到網頁https://breezometer.com/api/時,它返回預期的JSON答覆。如何找到根本原因:urllib2.URLError:<urlopen錯誤[SSL:CERTIFICATE_VERIFY_FAILED]證書驗證失敗(_ssl.c:590)?

然而,進入完全相同的數據 - 在模仿web請求(來自Python 2和3)以下Python腳本:

$ 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

我已經導入https://breezometer.com/api/https://breezometer.com/認證ats(以所有可用的格式)到Chrome - 遵循https://stackoverflow.com/a/31627786/1656850建議:仍然會出現CERTIFICATE_VERIFY_FAILED錯誤。

如何調試此問題的任何建議?

+0

這是沒有意義的證書導入到Chrome的問題是否與蟒蛇,因爲Python不從Chrome中使用證書。相反,您需要爲urlopen使用capath參數。 –

回答

0

使用Python 2.7添加驗證=假即

requests.get(url, headers=user_agent, verify=False) 
相關問題