0

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

4
  • 1
    Why don't you read the message sent back to you by the server? "You don't have permission to access..." BTW, wget works just fine. Commented Dec 27, 2019 at 4:35
  • @DYZ it's not about Permission at all, it's about User-Agent, check my answer below. Commented Dec 27, 2019 at 4:43
  • @αԋɱҽԃαмєяιcαη The last line of the example clearly says that the user doe not have permission (read beyond FileNotFoundError: [Errno 2] No such file or directory). The cause, indeed, is the same: the lack of the User-Agent Commented Dec 27, 2019 at 4:51
  • @DYZ if you taken a look about the reason of error FileNotFoundError: [Errno 2] No such file or directory it's actually due to fp = builtins.open(filename, "rb") where rb is equal to read byte which should be wb [write byte] ,and the real issue is on <TITLE>Access Denied</TITLE> which is due to User-Agent Commented Dec 27, 2019 at 4:54

1 Answer 1

3

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

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Still unable to download...I'm downloading multiple pictures at the same time but now its giving me respons.ok and 200 code but the file which download have 0 bytes size..
@mobinalhassan welcome to Stack Overflow! community, you have asked about one thing and i already replied according to it. Kindly please edit your question with the full details in order to be able to help you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.