2

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.

Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48
Small Atom
  • 164
  • 13

4 Answers4

3

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)
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
  • 1
    @SmallAtom You can read it from requests documents.`response.content` will return the binary content.So you can write it to your file directly. – jizhihaoSAMA Mar 16 '20 at 08:44
1

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.

Kris
  • 8,680
  • 4
  • 39
  • 67
  • But OP can not open the Image file,not the problem of gzip. – jizhihaoSAMA Mar 16 '20 at 08:35
  • The image content you download with .raw stream is not deflated. Storing that content as file will make the file corrupted. – Kris Mar 16 '20 at 08:38
  • I never said your code will not work!!!. I tried to explain what went wrong in the person's code. That is definitely the problem of gzip. If he still wants to work with streams instead of direct read ( not recommended for large files) , he will have to use a different way! – Kris Mar 16 '20 at 08:43
0

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.

Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48
0
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")
Lambo
  • 1,094
  • 11
  • 18