Skip to main content
 首页 » 编程设计

python之urllib.request.urlretrieve 尝试在 Python 中下载 jpeg 时出错

2024年10月01日9zhoujg

我正在尝试在 Python 3.5.2 中使用 urllib.request.urlretrieve(url, filename) 下载 .jpg 文件。网址是http://dm.victoriassecret.com/product/404x539/V603923_CROP1.jpg .引发以下错误: http.client.RemoteDisconnected: 远端关闭连接无响应。

尝试使用此 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] 时,我也遇到了问题.

在这种情况下会引发以下错误:引发 HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError:HTTP 错误 505:不支持 HTTP 版本

有人知道这些网址有什么问题吗?我该如何解决?与我分享你的知识,会很好。

请您参考如下方法:

Remote 没有响应,因为您的请求中缺少 header 。 此外,我建议您使用 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) 

现在打开您的工作目录,您会发现图像驻留在其中。

这也适用于您的其他链接。