工具支持单个下载,分类下载,以及全部下载。 考虑到有人没有编译环境附件给了编译后的exe文件可直接使用。 因为是chatgpt写的,所以有不清楚的地方可以直接发给它咨询
[Python] 纯文本查看 复制代码import tkinter as tk
from tkinter import messagebox
from concurrent.futures import ThreadPoolExecutor
import requests
import os
import random
from bs4 import BeautifulSoup
import threading
# 创建图片存储文件夹
os.makedirs('imgs', exist_ok=True)
def get_random_user_agent():
user_agents = [
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
"Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",
]
return random.choice(user_agents)
def download_image(img_url, img_title, category):
try:
headers = {'User-Agent': get_random_user_agent()}
img_resp = requests.get(img_url, headers=headers)
if img_resp.status_code == 200:
valid_img_title = "".join(c for c in img_title if c.isalnum() or c in (' ', '.', '_')).rstrip()
category_path = os.path.join('imgs', category) # Create category path
os.makedirs(category_path, exist_ok=True) # Create category folder
img_path = os.path.join(category_path, f"{valid_img_title}.jpg")
counter = 1
base_img_path, ext = os.path.splitext(img_path)
while os.path.exists(img_path):
img_path = f"{base_img_path}_{counter}{ext}"
counter += 1
with open(img_path, 'wb') as f:
f.write(img_resp.content)
print(f"Downloaded: {img_title} as {img_path}")
else:
print(f"Failed to download {img_title}")
except Exception as e:
print(f"Error downloading {img_title}: {e}")
def download_single_wallpaper(url):
try:
headers = {'User-Agent': get_random_user_agent()}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'lxml')
img_div = soup.find('div', class_='relative mb-3 float-left')
img_tag = img_div.find('img')
img_title = img_tag.get('alt')
img_url = img_tag.get('src')
# Here we need to determine the category for the single wallpaper
category = "单张" # You can replace this with a relevant category if applicable
download_image(img_url, img_title, category)
messagebox.showinfo("完成", f"单张壁纸 {img_title} 下载完成!")
except Exception as e:
print(f"Error downloading single wallpaper: {e}")
messagebox.showerror("错误", f"下载失败:{e}")
def start_single_download_thread():
url = entry_single_url.get().strip()
if not url:
messagebox.showwarning("警告", "请输入有效的壁纸链接!")
return
threading.Thread(target=download_single_wallpaper, args=(url,)).start()
def scrape_page(page, sort):
url = f'https://bizhi.cheetahfun.com/dn/c{sort}j/p{page}'
headers = {'User-Agent': get_random_user_agent()}
print(f"Requesting URL: {url}")
try:
resp = requests.get(url, headers=headers)
if resp.status_code == 404:
print(f"Page {page} not found. Stopping.")
return False
soup = BeautifulSoup(resp.text, 'html.parser')
img_divs = soup.find_all('div', class_='overflow-hidden relative rounded-sm w-79 mx-1 mb-3 h-40')
img_urls = []
for img_div in img_divs:
img_url = img_div.find('a').get('href')
img_title = img_div.find('a').get('title')
resp_child = requests.get(img_url, headers=headers)
soup_child = BeautifulSoup(resp_child.text, 'html.parser')
img_list_child = soup_child.find_all('div', class_='relative mb-3 float-left')
# Get the category from sort_mapping based on sort value
category = [key for key, value in sort_mapping.items() if value == sort][0]
img_urls.extend(
(img_child.find('img').get('src'), img_child.find('img').get('alt'), category)
for img_child in img_list_child
)
with ThreadPoolExecutor(max_workers=5) as executor:
executor.map(lambda img: download_image(img[0], img[1], img[2]), img_urls) # Pass category
except Exception as e:
print(f"Error scraping page {page}: {e}")
return True
def start_download(sort_values, start_page, end_page, download_all_pages):
loading_label.pack()
for sort in sort_values:
page = start_page
while True:
if not scrape_page(page, sort):
break
if not download_all_pages and page >= end_page:
break
page += 1
loading_label.pack_forget()
messagebox.showinfo("完成", "下载完成!")
def start_download_thread():
sort_values = [sort_mapping[label] for label, var in sort_vars.items() if var.get()]
if not sort_values:
messagebox.showwarning("警告", "请选择至少一个分类!")
return
try:
start_page = 1
end_page = None
if not var_all_pages.get():
start_page = int(entry_start_page.get())
end_page = int(entry_end_page.get())
if start_page
PixPin_2024-10-29_08-38-44.png (26.69 KB, 下载次数: 0)
下载附件
2024-10-29 08:38 上传
壁纸网址就在代码里面,就不贴出来了,因为分类全部下载会很占用资源,建议按需下载。
编译后文件下载地址:
https://koogg.lanzout.com/b0r9mz1xi
密码:7bsj