python入门--python获取网页图片

python入门--python获取网页图片_第1张图片

import requests
import bs4
import os
from bs4 import BeautifulSoup
url = "https://cc0.cn/image/renwu/";
save_folder = "huangliang";

def download_image(url, save_folder):
    response = requests.get(url)
    filename = os.path.join(save_folder, url.split("/")[-1])
    with open(filename, 'wb') as f:
        f.write(response.content)
        print(f"Downloaded {filename}")

if not os.path.exists(save_folder):
    os.makedirs(save_folder)

response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")

for img in soup.find_all("img"):
    src = img.get("src")
    if src.startswith("http"):
        download_image(src, save_folder)

你可能感兴趣的:(区块链,开发语言,python,前端)