[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 <= 0 or (end_page and end_page < start_page):
raise ValueError("页数范围无效")
threading.Thread(target=start_download, args=(sort_values, start_page, end_page, var_all_pages.get())).start()
except ValueError as e:
messagebox.showwarning("警告", f"请输入有效的页数!\n{e}")
def all_pages_selected(*args):
state = 'disabled' if var_all_pages.get() else 'normal'
entry_start_page.config(state=state)
entry_end_page.config(state=state)
# GUI部分
root = tk.Tk()
root.title("元气桌面静态壁纸下载器V1.1")
root.geometry("350x500")
root.configure(bg='#add8e6')
# Loading label
loading_label = tk.Label(root, text="正在下载,请稍候...", bg='#add8e6', fg='red')
# 输入单张壁纸URL
tk.Label(root, text="请输入单张静态壁纸网址:", bg='#add8e6').pack()
entry_single_url = tk.Entry(root, width=50)
entry_single_url.pack()
tk.Button(root, text="下载单张壁纸", command=start_single_download_thread, bg='lightblue').pack(pady=10)
# 分类复选框
tk.Label(root, text="请选择分类:", bg='#add8e6').pack()
frame_sorts = tk.Frame(root, bg='#add8e6')
frame_sorts.pack()
sort_labels = ["风景", "动漫", "美女", "游戏", "明星", "动物", "科幻", "小清新", "其他"]
sort_mapping = {label: number for label, number in zip(sort_labels, [1, 2, 3, 4, 5, 6, 7, 10, 11])} # 映射关系
sort_vars = {label: tk.BooleanVar() for label in sort_labels}
for i, label in enumerate(sort_labels):
tk.Checkbutton(frame_sorts, text=label, variable=sort_vars[label], bg='#add8e6').grid(row=i // 2, column=i % 2, padx=10, pady=5, sticky="w")
# 页数输入框
tk.Label(root, text="请输入页数范围:", bg='#add8e6').pack()
frame_pages = tk.Frame(root, bg='#add8e6')
frame_pages.pack()
tk.Label(frame_pages, text="开始页:", bg='#add8e6').grid(row=0, column=0)
entry_start_page = tk.Entry(frame_pages)
entry_start_page.grid(row=0, column=1)
tk.Label(frame_pages, text="结束页:", bg='#add8e6').grid(row=1, column=0)
entry_end_page = tk.Entry(frame_pages)
entry_end_page.grid(row=1, column=1)
# “全部页”复选框
var_all_pages = tk.BooleanVar()
var_all_pages.trace('w', all_pages_selected)
tk.Checkbutton(root, text="全部页", variable=var_all_pages, bg='#add8e6').pack()
# 开始按钮
tk.Button(root, text="开始下载", command=start_download_thread, bg='lightblue').pack(pady=20)
root.mainloop()