0
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

4
  • Unable to reproduce/ img = request.urlopen('https://i7y3a6q5.stackpathcdn.com/media/14490/%E0%A4%95-%E0%A4%B8-%E0%A4%A8.jpg?width=350&mode=max&animationprocessmode=first') returns http status code 200 which is success Commented Nov 27, 2019 at 9:14
  • If you try printing the content of img it will still print Error 404, I tried this method too. Here you won't receive any errors externally but you won't get any content either. It should return an object that contains that image. Commented Nov 27, 2019 at 9:19
  • can you post the actual error Commented Nov 27, 2019 at 9:25
  • <Response [404]> This is what I got when I tried printing the img variable. Commented Nov 27, 2019 at 9:33

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You so much this worked. But I couldn't understand how? i mean I'm practically doing the same thing, except the print statement that you mentioned in your code.
0

You should probably try this. It returns a 200 response and was able to print the content to console.

import requests

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

img = requests.get(url)
print(img.content)

You can write as this if you want it downloaded on your machine

import requests
import shutil

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

with open('image.jpg', 'wb') as output_file, requests.get(url, stream=True) as response:
    shutil.copyfileobj(response.raw, output_file)

3 Comments

<http.client.HTTPResponse object at 0x03A536D0> This is the type of content I need, this response object contains the data of that image.
I get the content printed in bytes to my console. Is this what you got when you printed img.content?
Yes, so do I need to cast the content? If yes, then how can I do that? Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.