python3.7 下使用 urllib 的request进行url 请求时候,如果请求的是https,请求可以会出现
出现了异常
源码如下import urllib.request
pic_url = 'https://img-ucdn-static.helijia.com/zmw/upload/element/20171129/dfa5bbf14cfa4d9bb1a1197e84a517f0.jpg'
name = pic_url.split('/')[-1]
local_pic_path = '/Users/tingfeng/Desktop/down_pic/' + name
urllib.request.urlretrieve(pic_url, local_pic_path)
执行代码出现异常Traceback (most recent call last):
File "/Users/tingfeng/PycharmProjects/python3/yugong/数据验证/down_pic_2.py", line 12, in
urllib.request.urlretrieve(pic_url, local_pic_path)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 247, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 525, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 543, in _open
'_open', req)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1360, in https_open
context=self._context, check_hostname=self._check_hostname)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1319, in do_open
raise URLError(err)
urllib.error.URLError:
解决方法
解决方法有两种,如下
方法1、命令行
在命令行中输入“/Applications/Python\ 3.7/Install\ Certificates.command”➜ ~ /Applications/Python\ 3.7/Install\ Certificates.command
-- pip install --upgrade certifi
Collecting certifi
Using cached https://files.pythonhosted.org/packages/18/b0/8146a4f8dd402f60744fa380bc73ca47303cccf8b9190fd16a827281eac2/certifi-2019.9.11-py2.py3-none-any.whl
Installing collected packages: certifi
Successfully installed certifi-2019.9.11
You are using pip version 19.0.3, however version 19.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
-- removing any existing file or link
-- creating symlink to certifi certificate bundle
-- setting permissions
-- update complete
方法2、界面安装
进入应用程序的Python 3.67目录,双击Install Certificates.command 进行安装,完毕后可以解决该问题(如下3.6为例)。
再次执行,问题解决
参考:https://stackoverflow.com/questions/40684543/how-to-make-python-use-ca-certificates-from-mac-os-truststore