I need to download an image from a url using Python. I'm using this to do so:
import requests
with requests.get(url, stream=True) as r:
with open(img_path, "wb") as f:
f.write(r.content)
In order for me to see the image in the browser, I need to be logged into my account on that site. The image may have been sent by someone else or myself.
The issue is that I am able to download some images successfully, but for other ones, I get an authentication error, i.e. that I'm not logged in.
In that case too, sometimes it downloads a file whose content is this:
{"result":"error","msg":"Not logged in: API authentication or user session required"}
And sometimes, it downloads the html file of the webpage which asks me to login to view the image.
Why am I getting this error for just some cases and not others? And how should I fix it?
try:
import requests
image_url = "https://www.python.org/static/community_logos/python-logo-master-v3-TM.png"
img_data = requests.get(image_url).content
with open('image_name.jpg', 'wb') as handler:
handler.write(img_data)
Note:
for authorization
from requests.auth import HTTPBasicAuth
img_data = requests.get('image_url', auth=HTTPBasicAuth('user', 'pass')).content
You can either use the response.raw file object, or iterate over the response.
import requests
import shutil
from requests.auth import HTTPBasicAuth
r = requests.get(url, auth=HTTPBasicAuth('user', 'pass'), stream=True)
if r.status_code == 200:
with open(path, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With