Python requests unable to download correctly a picture


I am unable to download a picture using python requests. The file downloaded seems to not be a jpg file. I don’t know why. Somebody has an idea ?

The code:

import requests
import shutil
link = ""
r = requests.get(link, verify=False,stream=True)
r.raw.decode_content = True
with open('img.jpg', 'wb') as f:
    shutil.copyfileobj(r.raw, f)

I just tried that URL, and what I got was a 403 response. It’s quite possible that you’re getting an HTML error page rather than the JPG image you requested. Try checking r.headers["Content-Type"] to see what yopu’re getting. Actually, check some of the other headers to see what it tells you about the response - I think it’ll be instructive.

1 Like

Oh yeah that was the issue. I did not try to open the file.
I just use a different user-agent and it works.