简体   繁体   English

无法在Python中使用“ urllib.request.urlretrieve”下载图像

[英]failing at downloading an image with “urllib.request.urlretrieve” in Python

If possible point the solution as well 如果可能的话也指出解决方案

My Code: 我的代码:

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")

The Error: 错误:

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1318, in do_open encode_chunked=req.has_header('Transfer-encoding')) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1239, in request self._send_request(method, url, body, headers, encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1285, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1234, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1026, in _send_output self.send(msg) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 964, in send self.connect() File "/Library/Frameworks/Python.fram 追溯(最近一次通话最近):文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,行1318,位于do_open encode_chunked = req.has_header('Transfer-编码))文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,第1239行,位于请求self._send_request(方法,URL,正文,标头,encode_chunked )文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py”,第1285行,位于_send_request self.endheaders(body,encode_chunked = encode_chunked)中,文件“ / Library / Frameworks /Python.framework/Versions/3.6/lib/python3.6/http/client.py“,第1234行,位于标题标头self._send_output(message_body,encode_chunked = encode_chunked)文件” /Library/Frameworks/Python.framework/Versions/ _send_output self.send(msg)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client”中的“ 3.6 / lib / python3.6 / http / client.py”,第1026行, .py”,第964行,位于send self.connect()文件“ /Library/Frameworks/Python.fram”中 ework/Versions/3.6/lib/python3.6/http/client.py", line 1400, in connect server_hostname=server_hostname) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 407, in wrap_socket _context=self, _session=session) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 814, in init self.do_handshake() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 1068, in do_handshake self._sslobj.do_handshake() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 689, in do_handshake self._sslobj.do_handshake() ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777) ework / Versions / 3.6 / lib / python3.6 / http / client.py“,行1400,在connect server_hostname = server_hostname中)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl .py“,第407行,位于wrap_socket _context = self,_session = session中)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py”,第814行,位于init self中。 do_handshake()文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py”,行1068,位于do_handshake 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)

During handling of the above exception, another exception occurred: 在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last): File "/Users/macintosh/PycharmProjects/Claculatorer/Bucky.py", line 9, in download_web_image(" https://upload.wikimedia.org/wikipedia/commons/b/b4/JPEG_example_JPG_RIP_100.jpg ") File "/Users/macintosh/PycharmProjects/Claculatorer/Bucky.py", line 7, in download_web_image urllib.request.urlretrieve(url, fullname) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 248, in urlretrieve with contextlib.closing(urlopen(url, data)) as fp: File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 223, in urlopen return opener.open(url, data, timeout) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 526, in open response = self._open(req, data) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 544, in _open '_open', req) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/u 追溯(最近一次通话):文件“ /Users/macintosh/PycharmProjects/Claculatorer/Bucky.py”,第9行,在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,全名)文件” /Library/Frameworks/Python.framework/Versions/3.6/ lib / python3.6 / urllib / request.py“,第248行,在urlretrieve中,具有contextlib.closing(urlopen(url,data))as fp:文件” /Library/Frameworks/Python.framework/Versions/3.6/lib/ python3.6 / urllib / request.py”,第223行,在urlopen中返回opener.open(URL,数据,超时)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/ “ request.py”,第526行,在打开的响应中= self._open(req,data)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第544行,在_open'_open'中,要求)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/u rllib/request.py", line 504, in _call_chain result = func(*args) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1361, in https_open context=self._context, check_hostname=self._check_hostname) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1320, in do_open raise URLError(err) urllib.error.URLError: _call_chain结果中的rllib / request.py”行504 = func(* args)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,行1361,在https_open context = self._context中,check_hostname = self._check_hostname)文件“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,行1320,在do_open中引发URLError( err)urllib.error.URLError:

Process finished with exit code 1 流程以退出代码1完成

Try this it works for me: 试试这个对我有用:

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"))

This outputs: None but it's fine! 输出: None但是很好!

The file is saved in the same directory as the script. 该文件与脚本保存在同一目录中。

在此处输入图片说明

Here is the way I ran the script: 这是我运行脚本的方式: 在此处输入图片说明

您只需要使用以下命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用urllib.request.urlretrieve下载需要固定的时间 - downloading with urllib.request.urlretrieve takes fixed time urllib.request.urlretrieve不通过HTTPS下载文件 - urllib.request.urlretrieve not downloading files over HTTPS 如何在python 2.7中使用urllib.request.urlretrieve - How can I use urllib.request.urlretrieve with python 2.7 单元测试模拟 urllib.request.urlretrieve() Python 3 和内部函数 - Unit test mock urllib.request.urlretrieve() Python 3 and internal function 尝试在Python中下载jpeg时出现urllib.request.urlretrieve错误 - urllib.request.urlretrieve ERROR trying to download jpeg in Python urllib.request.urlretrieve 在只从网站检索一张图片时卡住了 - urllib.request.urlretrieve got stuck when retrieved just one image from a site Python:urllib.request.urlretrieve保存一个空文件。 在其中写道“提供的id参数为空。” - Python: urllib.request.urlretrieve saves an empty file. Writes in it “Supplied id parameter is empty.” Python,基本问题:如何使用 urllib.request.urlretrieve 下载多个 url - Python, basic question: How do I download multiple url's with urllib.request.urlretrieve HTTP 错误 404:未找到 urllib.request.urlretrieve - HTTP Error 404: Not Found urllib.request.urlretrieve 使用什么命令代替 urllib.request.urlretrieve? - What command to use instead of urllib.request.urlretrieve?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM