我运行Python脚本已有一年多了,没有任何问题。几天前,我的计算机已重新启动。现在,Python 3.x在cmd提示符下出现urllib3和SSL证书的问题
此回溯在jupyter笔记本中不会发生:脚本在此处完美运行,并且在使用http而非https抓取网址时不会发生该脚本。
这些问题对我来说很陌生。首先,我检查Python的SSL证书
In [1]: import ssl
In [2]: print(ssl.OPENSSL_VERSION)
OpenSSL 1.1.0j 20 Nov 2018
然后,我尝试了another thread的回答,但未成功。
import warnings
from urllib3.exceptions import InsecureRequestWarning
warnings.simplefilter('ignore',InsecureRequestWarning)
在另一个thread中也发现了类似的问题,但答案仅适用于Mac。我使用Windows 10。
最后,我尝试在Jupyter Notebook中运行代码,它的工作原理很吸引人。没有回溯。仅从cmd提示符运行Python的回溯。
import urllib3
from bs4 import BeautifulSoup
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'}
url = https://whatever
response = http.request('GET', url, headers=hdr)
site = response.data
回溯:
Traceback (most recent call last):
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.website.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)')))