4.画像一括DL(BeautifulSoup)

Python
import os
import requests
from bs4 import BeautifulSoup


url = input('URL: ')
filename = input('FILENAME: ')


os.makedirs(r'C:/Users/9mlor/Downloads/' + filename)


r = requests.get(url)
bs = BeautifulSoup(r.content, 'html.parser')
imgs = bs.find_all('img')

imglists = []
for img in imgs:
    img = img.get('src')
    if img.startswith('//'):
        img = 'https:' + img
    if img.endswith('jpg') or img.endswith('png') or img.endswith('webp'):
        imglists.append(img)
            



for imglist in imglists:
    with open(r"C:/Users/9mlor/Downloads/" + filename + '/' + imglist.split('/')[-1], 'wb') as f:
        f.write(requests.get(imglist).content)

BACK