我对编程真的很陌生,目前正在 Youtube 上学习 python(“新波士顿 - 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')
Run Code Online (Sandbox Code Playgroud)
和错误:
回溯(最近一次调用最后一次): 文件“/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(方法、url、正文、标头、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 行,在结尾标题中 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 自发送(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 行,在连接中 服务器主机名=服务器主机名) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py”,第412行,在wrap_socket中 会话=会话 文件“/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.close(urlopen(url, data)) 作为 fp: 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 222 行,位于 urlopen 返回 opener.open(url, 数据, 超时) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”,第 525 行,打开 响应 = self._open(请求,数据) 文件“/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 context=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.URL错误: 进程已完成,退出代码为 1
我使用的是PyCharm 2018.3版本
请帮助我,谢谢
进入安装Python的文件夹。它的名称应该类似于 Python 3.x,其中 x 是您安装的 python 版本。现在双击“安装证书.命令”。以前遇到过这个错误,堆栈上的人也帮我修复了它。
我的路径如下:C:\Python33\Tools\Scripts
如果这不起作用,这里是使用 ssl 包的另一种解决方法:
pip install ssl
在运行代码之前执行此操作。然后将其添加到您的代码中。
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
Run Code Online (Sandbox Code Playgroud)
本质上,它的作用是使您的请求“安全”,因此 HTTPS 站点实际上会接受来自 python 的请求。在尝试访问带有 https 前缀的网站之前,您应该始终执行此操作。
归档时间: |
|
查看次数: |
2923 次 |
最近记录: |