Search code examples
pythonpython-3.xweb-scrapingurllib

urllib.error.HTTPError: HTTP Error 404: Not Found for a link that I can open in my browser


Iurl = 'https://i7y3a6q5.stackpathcdn.com/media/14490/क-स-न.jpg?width=350&mode=max&animationprocessmode=first'

The above url might generate an error as not all of the characters in it are in Unicode format. So, here's the converted url:

https://i7y3a6q5.stackpathcdn.com/media/14490/%E0%A4%95-%E0%A4%B8-%E0%A4%A8.jpg?width=350&mode=max&animationprocessmode=first

this is the url that is resulting in an error, it's a link to an image that I can open in my browser.

img = urllib.request.urlopen(Iurl)  # Downloading the image

This is the statement which is generating the 404 error. I tried the solutions provided on similar questions but none of them worked for me. I need something like this as my output when I print my img The ss contains the entire error stack trace enter image description here


Solution

  • The error you have given cannot be reproduced, you should show your code block and a copy of the error / stack trace. I have constructed a simple example of what you say your trying to do it and works fine for me.

    import urllib.request
    
    with open("img.jpg", 'wb') as image:
        Iurl = 'https://i7y3a6q5.stackpathcdn.com/media/14490/%E0%A4%95-%E0%A4%B8-%E0%A4%A8.jpg?width=350&mode=max&animationprocessmode=first'
        img = urllib.request.urlopen(Iurl)
        print(f"Fetching url {Iurl}, HTTP Response Code: {img.msg}({img.status})")
        image.write(img.read())
    

    CONSOLE OUTPUT

    Fetching url https://i7y3a6q5.stackpathcdn.com/media/14490/%E0%A4%95-%E0%A4%B8-%E0%A4%A8.jpg?width=350&mode=max&animationprocessmode=first, HTTP Response Code: OK(200)
    

    This creates a file in the dir where my code ran from. When I open the file the image is there.

    enter image description here