无法使用python url中的请求下载图像在响应中向我发送html? [英] unable to download image using request in python url send me html in respons?
问题描述
我无法使用不同的包下载图像我的代码
I'm unable to download images using different packages my code
url_h='https://i8.amplience.net/i/nlyscandinavia/086399-0001_01/i-light-rib-polo-top/'
response=requests.get(url_h,stream=True).content
with open('pic2.jpg', 'wb') as handle:
handle.write(response)
第二种方法
img = Image.open(io.BytesIO(response))
我尝试了不同的解决方案,但无法使用 wget、urllib 等,但无法在 429 中打印响应
I have tried different solution but not working wget, urllib etc but not working printing response in 429
Traceback (most recent call last):
File "/home/mobin/PycharmProjects/nelly/source/test.py", line 23, in <module>
aux_im = Image.open(requests.get(url_h, stream=True).content)
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2548, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: b'<HTML><HEAD>\n<TITLE>Access Denied</TITLE>\n</HEAD><BODY>\n<H1>Access Denied</H1>\n \nYou don\'t have permission to access "http://i8.amplience.net/i/nlyscandinavia/086399-0001_01/i-light-rib-polo-top/" on this server.<P>\nReference #18.87647b5c.1577361511.23f0f869\n</BODY>\n</HTML>\n'
编辑
推荐答案
实际上你得到的是响应代码 429 分配给Too Many Requests
,描述为用户在给定时间内发送了太多请求.用于限速方案
Actually you were getting response code 429 which is assigned to Too Many Requests
with description of The user has sent too many requests in a given amount of time. Intended for use with rate-limiting schemes
但在您的情况下,可以通过 User-Agent
绕过 Headers
,您不需要使用 stream=True
以及因为它已经是一个开放的流网址.
But in your case it can be Bypassed via User-Agent
to be in Headers
, you don't need to use stream=True
as well since it's already an open stream url.
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0'}
r = requests.get(
"https://i8.amplience.net/i/nlyscandinavia/086399-0001_01/i-light-rib-polo-top/", headers=headers)
with open("photo.jpg", 'wb') as f:
f.write(r.content)
这篇关于无法使用python url中的请求下载图像在响应中向我发送html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!