mac python 图_Mac 环境 Python3.7 使用 urllib.request 下载图片

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为例)。

loading.gif

再次执行,问题解决

参考:https://stackoverflow.com/questions/40684543/how-to-make-python-use-ca-certificates-from-mac-os-truststore

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值