在Python中使用“ urllib.request.urlretrieve”下载图像失败

时间:2018-10-09 09:10:18

标签: python python-3.x ssl web-crawler ssl-certificate

如果可能的话,也要指出解决方案

我的代码:

import random
import urllib.request

def download_web_image(url):
    name = random.randrange(1,1000)
    fullname = str(name) + ".jpg"
    urllib.request.urlretrieve(url, fullname)

download_web_image("https://upload.wikimedia.org/wikipedia/commons/b/b4/JPEG_example_JPG_RIP_100.jpg")

错误:

  

回溯(最近通话最近):     在do_open中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第1318行       encode_chunked = req.has_header('Transfer-encoding'))     请求中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,第1239行       self._send_request(方法,URL,正文,标头,encode_chunked)     _send_request中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,行1285       self.endheaders(body,encode_chunked = encode_chunked)     文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,行1234,在标题中       self._send_output(消息正文,encode_chunked = encode_chunked)     _send_output中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,行1026       self.send(msg)     发送中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,行964       self.connect()     连接中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,行1400       server_hostname =服务器主机名)     wrap_socket中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py”,第407行       _context =自我,_session =会话)      init 中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py”,第814行       self.do_handshake()     在do_handshake中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py”,行1068       self._sslobj.do_handshake()     文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py”,行689,位于do_handshake中       self._sslobj.do_handshake()   ssl.SSLError:[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败(_ssl.c:777)

     

在处理上述异常期间,发生了另一个异常:

     

回溯(最近通话最近):     在第9行的“ /Users/macintosh/PycharmProjects/Claculatorer/Bucky.py”文件中       download_web_image(“ https://upload.wikimedia.org/wikipedia/commons/b/b4/JPEG_example_JPG_RIP_100.jpg”)     文件“ /Users/macintosh/PycharmProjects/Claculatorer/Bucky.py”,第7行,在download_web_image中       urllib.request.urlretrieve(URL,全名)     在urlretrieve中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第248行       使用contextlib.closing(urlopen(url,data))作为fp:     在urlopen中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第223行       返回opener.open(URL,数据,超时)     打开文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第526行       响应= self._open(req,数据)     _open中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,行544       '_open',要求)     _call_chain中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第504行       结果= func(* args)     在https_open中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第1361行       context = self._context,check_hostname = self._check_hostname)     在do_open中的文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第1320行       引发URLError(err)   urllib.error.URLError:

     

以退出代码1完成的过程

2 个答案:

答案 0 :(得分:0)

尝试一下对我有用:

import random
import urllib.request

def download_web_image(url):
    name = random.randrange(1,1000)
    fullname = str(name) + ".jpg"
    urllib.request.urlretrieve(url, fullname)

print(download_web_image("https://upload.wikimedia.org/wikipedia/commons/b/b4/JPEG_example_JPG_RIP_100.jpg"))

这将输出:None,但是很好!

文件与脚本保存在同一目录中。

enter image description here

这是我运行脚本的方式: enter image description here

答案 1 :(得分:0)

您只需要使用以下命令:

 /Applications/Python\ 3.6/Install\ Certificates.command