Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading images using requests in Python3

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?

like image 908
doobeedoobee Avatar asked Jul 16 '19 10:07

doobeedoobee


2 Answers

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
like image 196
ncica Avatar answered Sep 20 '22 23:09

ncica


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)  
like image 38
Muhammad Haseeb Avatar answered Sep 19 '22 23:09

Muhammad Haseeb