python-3.x - Python 3.7 - 下载图像 - Urllib.request.urlretrieve 错误

标签 python-3.x

我对编程真的很陌生,目前正在 Youtube 上学习 Python('The New Boston - Python 3')
试图根据视频中显示的代码从互联网下载图像,但弹出错误。
这是代码:

import random
import urllib.request

def download_web_image(url):
    name = random.randrange(1,1000)
    full_name = str(name) + '.gif'  #str convert number to word
    urllib.request.urlretrieve(url, full_name)

download_web_image ('https://images.freeimages.com/images/large-previews/ed3/a-stormy-paradise-1-1563744.jpg')

和错误:

回溯(最近一次调用最后一次):
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 1317 行,在 do_open
encode_chunked=req.has_header('传输编码'))
请求中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”,第 1229 行
self._send_request(method, url, body, headers, encode_chunked)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”,第 1275 行,在 _send_request 中
self.endheaders(主体,encode_chunked=encode_chunked)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”,第1224行,在endheaders中
self._send_output(message_body, encode_chunked=encode_chunked)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”,第 1016 行,在 _send_output 中
self.send(msg)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”,第 956 行,发送
self.connect()
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”,第 1392 行,在连接中
server_hostname=server_hostname)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py”,第 412 行,在 wrap_socket
session = session
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py”,第 853 行,在 _create 中
self.do_handshake()
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py”,第 1117 行,在 do_handshake 中
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:证书链中的自签名证书 (_ssl.c:1056)

在处理上述异常的过程中,又发生了一个异常:

回溯(最近一次调用最后一次):
文件“/Users/isaactai/PycharmProjects/isaacfirst/IMAGEDOWNLOAD.py”,第 10 行,在
download_web_image ('https://images.freeimages.com/images/large-previews/ed3/a-stormy-paradise-1-1563744.jpg')
文件“/Users/isaactai/PycharmProjects/isaacfirst/IMAGEDOWNLOAD.py”,第 8 行,在 download_web_image
urllib.request.urlretrieve(url, full_name)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 247 行,在 urlretrieve 中
使用 contextlib.closure(urlopen(url, data)) 作为 fp:
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 222 行,在 urlopen
返回 opener.open(url, data, timeout)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第525行,打开
response = self._open(req, data)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 543 行,在 _open
'_open',请求)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 503 行,在 _call_chain
结果 = func(*args)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 1360 行,在 https_open
上下文=self._context,check_hostname=self._check_hostname)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 1319 行,在 do_open
引发 URLError(err)
urllib.error.URLError:

进程以退出代码 1 结束

我使用的是 PyCharm 2018.3 版本

请帮帮我,谢谢

最佳答案

转到安装 Python 的文件夹。它应该有一个类似于 Python 3.x 的名称,其中 x 是您安装的任何版本的 Python。现在双击“安装 Certificates.command”。之前有过这个错误,堆栈上的某个人也帮我修复了它。

我的路径如下:
C:\Python33\工具\脚本

如果这不起作用,则是使用 ssl 包的另一种解决方法:
pip install ssl
在运行代码之前执行此操作。然后将其添加到您的代码中。

import ssl

ssl._create_default_https_context = ssl._create_unverified_context

本质上,这样做是使您的请求“secure”,因此 HTTPS 站点实际上会接受来自 python 的请求。在尝试访问带有 https 前缀的站点之前,您应该始终执行此操作。

关于python-3.x - Python 3.7 - 下载图像 - Urllib.request.urlretrieve 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54472026/

相关文章:

python - 有条件地填充 pandas 数据框会导致空数据框

python-3.x - `form.validate_on_submit()` 和 `form.validate()` 的区别

python - 'utf- 8' codec can' t 解码字节读取 Python3.4 中的文件,但不是 Python2.7

python-3.x - 有没有办法在每个特定的时间间隔运行 python Flask 函数并在本地服务器上显示输出?

python-3.x - 在 jupyter notebook 中导入本地模块

python - 如何在 matplotlib python 中设置确切的日期刻度

python-3.x - 极地 : Naming Dataframe Columns when using scan_csv on headerless CSVs

python - pyenv 使用正确的 python 版本但使用错误的库文件夹

python - 使用 python 3 将列表添加为树的子级

python-3.x - 计算两个数组列的成对总和