urllib.erro.URLError
錯誤信息
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:748)>
由字面可知該錯誤是由于無法進行SSL驗證所致
解決方法
查閱Python3.6的版本信息可知Mac二進制版本忽略了對SSL證書的驗證,原文1如下:
macOS users: If you are using the Python 3.6 from the python.org binary installer linked on this page, please carefully read the Important Information displayed during installation; this information is also available after installation by clicking on /Applications/Python 3.6/ReadMe.rtf. There is important information there about changes in the 3.6.0 installer-supplied Python, particularly with regard to SSL certificate validation.
在路徑 /Applications/Python 3.6
下的 ReadMe.rft
中提到:
This variant of Python 3.6 now includes its own private copy of OpenSSL 1.0.2. Unlike previous releases, the deprecated Apple-supplied OpenSSL libraries are no longer used. This also means that the trust certificates in system and user keychains managed by the Keychain Access application and the security command line utility are no longer used as defaults by the Python ssl module. For 3.6.0, a sample command script is included in /Applications/Python 3.6 to install a curated bundle of default root certificates from the third-party certifi package (https://pypi.python.org/pypi/certifi). If you choose to use certifi, you should consider subscribing to the project's email update service to be notified when the certificate bundle is updated.
與之前的版本不同,該版本不再使用蘋果提供的OpenSSL庫,用戶可選擇第三方的certifi包:
- 目錄
/Applications/Python 3.6
提供了安裝腳本Install Certificates.command
,直接運行腳本即可。 - 或使用命令
pip3 install certi
進行安裝