[英]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.