简体   繁体   English

尝试在Python中下载jpeg时出现urllib.request.urlretrieve错误

[英]urllib.request.urlretrieve ERROR trying to download jpeg in Python

I am trying to download a .jpg file, using urllib.request.urlretrieve(url, filename) in Python 3.5.2. 我正在尝试使用Python 3.5.2中的urllib.request.urlretrieve(url,filename)下载.jpg文件。 The url is http://dm.victoriassecret.com/product/404x539/V603923_CROP1.jpg . 网址为http://dm.victoriassecret.com/product/404x539/V603923_CROP1.jpg The following error raises: http.client.RemoteDisconnected: Remote end closed connection without response . 引发以下错误: http.client.RemoteDisconnected:远程终端关闭了连接,没有响应

I also have a problem when trying the same with this url = http://lp2.hm.com/hmprod?set=source[/model/2017/9AS 0505882 002 00 0034.jpg],type[STILLLIFE_FRONT]&hmver=0&call=url[file:/product/style] . 使用此网址尝试同样的操作时,我也遇到问题= http://lp2.hm.com/hmprod?set=source[/model/2017/9AS 0505882 002 00 0034.jpg],类型[STILLLIFE_FRONT]&hmver = 0&call = url [file:/ product / style]

In that case the following error raises: raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 505: HTTP Version not supported 在这种情况下,将引发以下错误: 引发HTTPError(req.full_url,code,msg,hdrs,fp)urllib.error.HTTPError:HTTP错误505:不支持HTTP版本

Does anybody know what is the problem with these urls and how could I fix it? 有谁知道这些网址有什么问题,我该如何解决? Sharing your knowledge with me, would be nice. 与我分享您的知识,将非常高兴。

The remote isn't responding because you're lacking headers in your request. 遥控器没有响应,因为您的请求中缺少标头。 Furthermore, I suggest you use the requests module (install it via pip install requests ), as it's way better and faster than urllib : 此外,我建议您使用requests模块(通过pip install requests进行pip install requests ),因为它比urllib更好,更快:

import requests
headers = headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Cafari/537.36'}

pic = requests.get('http://dm.victoriassecret.com/product/404x539/V603923_CROP1.jpg', headers=headers)

with open('beautiful.jpg', 'wb') as photo:
    photo.write(pic.content)

Now open your working directory and you'll find the image residing there. 现在打开您的工作目录,您将找到驻留在其中的图像。

This will also work with your other link. 这也将与您的其他链接一起使用。

暂无
暂无

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

相关问题 Python,基本问题:如何使用 urllib.request.urlretrieve 下载多个 url - Python, basic question: How do I download multiple url's with urllib.request.urlretrieve 如何在python 2.7中使用urllib.request.urlretrieve - How can I use urllib.request.urlretrieve with python 2.7 HTTP 错误 404:未找到 urllib.request.urlretrieve - HTTP Error 404: Not Found urllib.request.urlretrieve 无法在Python中使用“ urllib.request.urlretrieve”下载图像 - failing at downloading an image with “urllib.request.urlretrieve” in Python 单元测试模拟 urllib.request.urlretrieve() Python 3 和内部函数 - Unit test mock urllib.request.urlretrieve() Python 3 and internal function urllib.error.URLError: - urllib.error.URLError: <urlopen error [WinError 10060] using urllib.request.urlretrieve() on Windows 10 with Python3 为什么会出现错误:没有名为“ urllib.request.urlretrieve”的模块; &#39;urllib.request&#39;不是一个软件包 - Why it gives me the error: No module named 'urllib.request.urlretrieve'; 'urllib.request' is not a package Python:urllib.request.urlretrieve保存一个空文件。 在其中写道“提供的id参数为空。” - Python: urllib.request.urlretrieve saves an empty file. Writes in it “Supplied id parameter is empty.” 使用urllib.request.urlretrieve下载需要固定的时间 - downloading with urllib.request.urlretrieve takes fixed time 使用什么命令代替 urllib.request.urlretrieve? - What command to use instead of urllib.request.urlretrieve?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM