简体   繁体   中英

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)

2nd method

img = Image.open(io.BytesIO(response))

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&#58;&#47;&#47;i8&#46;amplience&#46;net&#47;i&#47;nlyscandinavia&#47;086399&#45;0001&#95;01&#47;i&#45;light&#45;rib&#45;polo&#45;top&#47;" on this server.<P>\nReference&#32;&#35;18&#46;87647b5c&#46;1577361511&#46;23f0f869\n</BODY>\n</HTML>\n'

Edit

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 The user has sent too many requests in a given amount of time. Intended for use with rate-limiting schemes

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM