Skip to content

Instantly share code, notes, and snippets.

@Neko288
Created December 12, 2021 04:15
Show Gist options
  • Save Neko288/a5fdc774a1854b75feeefb65d5a0e44e to your computer and use it in GitHub Desktop.
Save Neko288/a5fdc774a1854b75feeefb65d5a0e44e to your computer and use it in GitHub Desktop.
サイトの画像を全部DL
import requests, winsound
from bs4 import BeautifulSoup
import os
#画像のURL構造がサイトによって異なるので、エラーが起きたら微修正お願いします。よくあるエラーは24行目当たりのrequests.getです。
path = ('C:/保存する画像のパス')
def save_image():
URL = ('http://XXXX')
try:
os.mkdir("img")
except:
pass
soup = BeautifulSoup(requests.get(URL).content,'lxml')
imgs = []
for link in soup.find_all("img"):
if link.get("src").endswith(".jpg"):
imgs.append(link.get("src"))
elif link.get("src").endswith(".png"):
imgs.append(link.get("src"))
elif link.get("src").endswith(".webp"):
imgs.append(link.get("src"))
for target in imgs:
re = requests.get('http://'+target)
with open(path+'/'+''+target.split('/')[-1], 'wb') as f:
f.write(re.content)
print("オワタヨ"+'\n')
winsound.Beep(294,1000)
while True:
save_image()
input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment