如何使用python下载包括gif文件在内的所有图像

如何使用python下载包括gif文件在内的所有图像,python,image,python-3.x,beautifulsoup,Python,Image,Python 3.x,Beautifulsoup,我使用这段代码下载jpg文件没有任何问题。但正如您所看到的,下面的页面源代码包含了大量带有path blank.gif的图像 <a href="/en/chowchow-puppy-sleeping-dogs-pet-448311/"><img src="/static/img/blank.gif" 更新版本。阅读评论了解更多信息 import random import requests from bs4 import BeautifulSoup # got from h

我使用这段代码下载jpg文件没有任何问题。但正如您所看到的,下面的页面源代码包含了大量带有path blank.gif的图像

<a href="/en/chowchow-puppy-sleeping-dogs-pet-448311/"><img src="/static/img/blank.gif"

更新版本。阅读评论了解更多信息

import random
import requests
from bs4 import BeautifulSoup

# got from http://stackoverflow.com/a/16696317
def download_file(url):
    local_filename = url.split('/')[-1]
    print("Downloading {} ---> {}".format(url, local_filename))
    # NOTE the stream=True parameter
    r = requests.get(url, stream=True)
    with open(local_filename, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024):
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)
    return local_filename

def Download_Image_from_Web(url):
    source_code = requests.get(url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text, "html.parser")
    for link in soup.findAll('img'):
        image_links = link.get('src')
        if image_links.endswith('blank.gif'):
            image_links = link.get('data-lazy')
        if not image_links.startswith('http'):
            image_links = url + '/' + image_links
        download_file(image_links)

Download_Image_from_Web("https://pixabay.com/en/photos/?q=sleeping+puppy&hp=&image_type=&cat=&min_width=&min_height=")

更新版本。阅读评论了解更多信息

import random
import requests
from bs4 import BeautifulSoup

# got from http://stackoverflow.com/a/16696317
def download_file(url):
    local_filename = url.split('/')[-1]
    print("Downloading {} ---> {}".format(url, local_filename))
    # NOTE the stream=True parameter
    r = requests.get(url, stream=True)
    with open(local_filename, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024):
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)
    return local_filename

def Download_Image_from_Web(url):
    source_code = requests.get(url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text, "html.parser")
    for link in soup.findAll('img'):
        image_links = link.get('src')
        if image_links.endswith('blank.gif'):
            image_links = link.get('data-lazy')
        if not image_links.startswith('http'):
            image_links = url + '/' + image_links
        download_file(image_links)

Download_Image_from_Web("https://pixabay.com/en/photos/?q=sleeping+puppy&hp=&image_type=&cat=&min_width=&min_height=")

我可以问你为什么在这个函数中放一个特定的url吗?下载的时候我应该把文件换成不同的名字,对吗?哦,看来我没有仔细阅读你的问题。我决定用这个小狗jpeg替换任何blank.gif。但现在我又检查了一次你的url,看起来当'src'包含'blank.gif'时,你可以只使用'data lazy'属性。所以,请在我的回答中检查更新的代码。在另外一个条件下确实有效。谢谢我可以问你为什么在这个函数中放一个特定的url吗?下载的时候我应该把文件换成不同的名字,对吗?哦,看来我没有仔细阅读你的问题。我决定用这个小狗jpeg替换任何blank.gif。但现在我又检查了一次你的url,看起来当'src'包含'blank.gif'时,你可以只使用'data lazy'属性。所以,请在我的回答中检查更新的代码。在另外一个条件下确实有效。谢谢