简体   繁体   English

从 URL Python 下载图像

[英]Download images from URL Python

I have problem with my script when i try download images from web url.当我尝试从 web url 下载图像时,我的脚本有问题。 It works on other page (offex.pl) but in my shop images are not working.它适用于其他页面 (offex.pl),但在我的商店中图片不起作用。 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例如( https://offex.pl/images/detailed/11/94102_jeep_sbhn-8h.jpg )下载OK

but for my shop url https://sw19048.smartweb-static.com/upload_dir/shop/misutonida_ec-med-384-ix.jpg is not working.但是对于我的商店网址https://sw19048.smartweb-static.com/upload_dir/shop/misutonida_ec-med-384-ix.jpg不起作用。

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)

The issue is with the URL which you are using to download.问题在于您用于下载的 URL。 Its not an issue, but a difference from other URL you have mentioned.这不是问题,而是与您提到的其他 URL 不同。

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. URL https://offex.pl/images/detailed/11/94102_jeep_sbhn-8h.jpg返回一个没有任何压缩的图像作为响应。

回复

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.另一方面,商店 URL https://sw19048.smartweb-static.com/upload_dir/shop/misutonida_ec-med-384-ix.jpg返回在标题中启用gzip压缩的图像。

UR 的响应

So the raw response you get is compressed with gzip compression.所以你得到的原始响应是用 gzip 压缩来压缩的。 You can decompress the response with gzip, if you know the compression is always gzip like below您可以使用 gzip 解压缩响应,如果您知道压缩始终是 gzip,如下所示

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.或者您可以使用替代库,如urllib2或类似的库,它们负责解压。 I was trying to explain why it failed for your URL , but not for other.我试图解释为什么它对您的 URL 失败,而不是其他人。 Hope this makes sense.希望这是有道理的。

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.我认为问题在于所用库的请求响应。

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM