Small Atom
Small Atom

Reputation: 164

Download images from URL Python

I have problem with my script when i try download images from web url. It works on other page (offex.pl) but in my shop images are not working. i just have all files but i can't open Files

my code:

import os
import time
import requests
from termcolor import colored

def get_folder(url):
    all_folders= os.path.dirname(url)
    folder=os.path.basename(all_folders)
    return folder
def filename(url):
    file=url[url.rfind("/") + 1:]
    return file

def download(link):
    error = []
    ok = 0
    fail = 0


    root_folder = get_folder(link)
    path = "{}/{}".format("download", root_folder)
    if not os.path.exists(path):
        os.makedirs(path)
    url = link
    file = filename(link)

    result = requests.get(url, stream=True)

    completeName = os.path.join("download", root_folder, file)
    print(completeName)
    if result.status_code == 200:
        image = result.raw.read()
        open(completeName, "wb").write(image)
        ok += 1
        succes = "{} {} {}".format(ok, colored("Pobrano:", "green"), url)
        print(succes)

        time.sleep(1)

    else:
        found_error = "{} {}".format(colored("Brak pliku!:", "red"), url)
        print(found_error)
        fail += 1
        error.append("ID:{} NUMBER:{} link: {}".format(id, url))
    with open("log.txt", "w") as filehandle:
        for listitem in error:
            filehandle.write('%s\n' % listitem)
    print(colored("Pobrano plików: ", "green"), ok)
    print(colored("Błędy pobierania: ", "red"), fail)

img_url="https://sw19048.smartweb-static.com/upload_dir/shop/misutonida_ec-med-384-ix.jpg"


download(img_url)

What Im doing wrong?

for example (https://offex.pl/images/detailed/11/94102_jeep_sbhn-8h.jpg) download OK

but for my shop url https://sw19048.smartweb-static.com/upload_dir/shop/misutonida_ec-med-384-ix.jpg is not working.

Upvotes: 2

Views: 1811

Answers (4)

Lambo
Lambo

Reputation: 1174

from io import BytesIO
import requests
from PIL import Image

fileRequest = requests.get("https://sw19048.smartweb-static.com/upload_dir/shop/misutonida_ec-med-384-ix.jpg")
doc = Image.open(BytesIO(fileRequest.content))
doc.save("newFile.jpg")

Upvotes: 0

Kris
Kris

Reputation: 8868

The issue is with the URL which you are using to download. Its not an issue, but a difference from other URL you have mentioned.

Let me explain

The URL https://offex.pl/images/detailed/11/94102_jeep_sbhn-8h.jpg returns an image as response with out any compression.

Response

On the other hand, the shop URL https://sw19048.smartweb-static.com/upload_dir/shop/misutonida_ec-med-384-ix.jpg returns the image with gzip compression enabled in the headers.

Response for UR

So the raw response you get is compressed with gzip compression. You can decompress the response with gzip, if you know the compression is always gzip like below

import gzip
import io

image = result.raw.read()
buffer = io.BytesIO(image)
deflatedContent = gzip.GzipFile(fileobj=buffer)
open("D:/sample.jpg", "wb").write(deflatedContent.read())

Or you can use alternative libraries like urllib2 or similar ones, which takes care of decompression. I was trying to explain why it failed for your URL , but not for other. Hope this makes sense.

Upvotes: 1

jizhihaoSAMA
jizhihaoSAMA

Reputation: 12672

If you want to use requests module,you can use this:

import requests
response = requests.get("https://sw19048.smartweb-static.com/upload_dir/shop/misutonida_ec-med-384-ix.jpg")
with open('./Image.jpg','wb') as f:
    f.write(response.content)

Upvotes: 3

Nilanka Manoj
Nilanka Manoj

Reputation: 3718

try :

import urllib2

def download_web_image(url):
    request = urllib2.Request(url)
    img = urllib2.urlopen(request).read()
    with open('test.jpg', 'wb') as f:
        f.write(img)

download_web_image("https://sw19048.smartweb-static.com/upload_dir/shop/misutonida_ec-med-384-ix.jpg")

It is working for your URL. I think the issue is with the request response of the used library.

Upvotes: 0

Related Questions