Answer the question
In order to leave comments, you need to log in
What is the most efficient way to download a png image to hard drive from a python link?
I have tried the following way
import requests
img_url = 'http://markof.fun/im/1.png'
r = requests.get(img_url)
out = open('d:/o/temp.png', "wb")
out.write(r.content)
out.close()
Answer the question
In order to leave comments, you need to log in
So you don’t have a picture if you make such a request. Server 403 returns. Check response status
import requests
img_url = 'http://markof.fun/im/1.png'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'}
r = requests.get(img_url, headers=headers)
if r.status_code == 200:
with open('temp.png', 'wb') as f:
f.write(r.content)
else:
print('Error', r.status_code)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question