报错原因
当使用urllib.urlopen打开一个https链接时,需要先验证一下SSL证书,如果无法验证证书,就会报错:
<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)>
解决方法
通过在urlopen中需传入上下文参数:context
具体代码如下:
import urllib.request
import ssl
context = ssl._create_unverified_context()
url = 'https://www.python.org/'
response = urllib.request.urlopen(url,context=context)
print(response.read().decode('utf-8'))