urllib.request.urlretrieve 在 Python 中尝试下载 jpeg 时出错
urllib.request.urlretrieve ERROR trying to download jpeg in Python
我正在尝试使用 urllib.request.urlretrieve(url, filename) 在 Python 3.5.2 中下载 .jpg 文件。 url 是 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] 时也遇到了问题。
在这种情况下会引发以下错误:raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError:HTTP 错误 505:不支持 HTTP 版本
有人知道这些 url 有什么问题吗?我该如何解决?与我分享你的知识,会很好。
遥控器没有响应,因为您的请求中缺少 headers。
此外,我建议您使用 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)
现在打开您的工作目录,您会发现图像驻留在其中。
这也适用于您的其他 link。
我正在尝试使用 urllib.request.urlretrieve(url, filename) 在 Python 3.5.2 中下载 .jpg 文件。 url 是 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] 时也遇到了问题。
在这种情况下会引发以下错误:raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError:HTTP 错误 505:不支持 HTTP 版本
有人知道这些 url 有什么问题吗?我该如何解决?与我分享你的知识,会很好。
遥控器没有响应,因为您的请求中缺少 headers。
此外,我建议您使用 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)
现在打开您的工作目录,您会发现图像驻留在其中。
这也适用于您的其他 link。