吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 10654|回复: 182
收起左侧

[Windows] 元气桌面静态壁纸下载器 V1.0

    [复制链接]
koogg 发表于 2024-10-29 10:35
本帖最后由 koogg 于 2024-10-29 11:34 编辑

找壁纸的时候遇到元气桌面壁纸,有些壁纸看起来不错,但是网页不给下载,于是使用python抓取了一下网页,发现在网页源文件就有,只是动态壁纸的分辨率太低,就留下静态壁纸了,
工具支持单个下载,分类下载,以及全部下载。 考虑到有人没有编译环境附件给了编译后的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 <= 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()


PixPin_2024-10-29_08-38-44.png

壁纸网址就在代码里面,就不贴出来了,因为分类全部下载会很占用资源,建议按需下载。

编译后文件下载地址:
https://koogg.lanzout.com/b0r9mz1xi
密码:7bsj

免费评分

参与人数 40吾爱币 +32 热心值 +36 收起 理由
一次过 + 1 + 1 谢谢@Thanks!
潇未然 + 1 + 1 谢谢@Thanks!
lgeae + 1 + 1 热心回复!
远水涵秋 + 1 我很赞同!
xiaozi2023 + 1 + 1 我很赞同!
cchj + 1 我很赞同!
qingchengshuang + 1 + 1 我很赞同!
hysxm666 + 1 + 1 我很赞同!
hjh422 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
HhhTtt28 + 1 谢谢@Thanks!
Wembanyama + 1 可以可以
syhtiepi + 1 我很赞同!
yeeeee + 1 + 1 热心回复!
DregMan + 1 + 1 热心回复!
苍白之白 + 1 谢谢@Thanks!
ly_1996 + 1 谢谢@Thanks!
xmsumi + 1 我很赞同!
fuyin520 + 1 我需要下载动态壁纸的方法!还有一个更简单的方法,浏览器的资源嗅探功能! ...
xsfh1978 + 1 + 1 谢谢@Thanks!
mrdoctorc + 1 + 1 我很赞同!
lijinfeng043 + 1 + 1 谢谢@Thanks!
风中漫步 + 1 + 1 谢谢@Thanks!
hangyick999 + 1 谢谢@Thanks!
cgq0404 + 1 + 1 我很赞同!
刀下生、刀下死 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
junjia215 + 1 + 1 用心讨论,共获提升!
爱的太迟 + 1 + 1 谢谢@Thanks!
肖克申的救赎 + 1 + 1 谢谢@Thanks!
dogox + 1 + 1 我很赞同!
huojingang + 1 + 1 热心回复!
wyakuan + 1 + 1 谢谢@Thanks!
yanglinman + 1 谢谢@Thanks!
awdxszz + 1 + 1 谢谢@Thanks!
呼噜哥 + 1 + 1 能分享源码的一律好评
梦古无疆 + 1 + 1 谢谢@Thanks!
syy051120 + 1 + 1 我很赞同!
maybe1229 + 1 谢谢@Thanks!
macus + 1 + 1 谢谢@Thanks!
lp-cg + 1 + 1 谢谢@Thanks!
醉生梦死. + 2 + 1 刚好需要,刚好看到!感谢楼主分享!!!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

潇潇暮雨寒 发表于 2024-10-29 11:44
下载到哪里去了,怎么找不到呢
 楼主| koogg 发表于 2024-11-2 08:48
xsfh1978 发表于 2024-11-2 06:17
还要安装一个元气桌面,怕的 就是这种东西

这个不需要呀,就是不想下载,所以直接抓取网页获取,附件里面只是代码生成的exe可执行文件,方便没编译环境的使用。有环境的运行代码也一样的效果
 楼主| koogg 发表于 2024-11-2 18:42
xsfh1978 发表于 2024-11-2 17:54
真的可以下载,谢谢大佬。可是里面的图片虽然高清,感觉是ai做的图吧?

这个,,下载的只是网站的图,至于网站的图是怎么做的,咱管不了啊
lp-cg 发表于 2024-10-29 11:05
感谢分享,收下了
awdxszz 发表于 2024-10-29 17:07
试试看,感谢大佬分享
changyufeichang 发表于 2024-10-29 10:43
好东西,下载下来研究下,感谢
醉生梦死. 发表于 2024-10-29 10:52
非常实用且不错的一款工具,如果能分类储存则更完美了!期待下一个版本!感谢分享!
JJ20160225 发表于 2024-10-29 11:13
学习学习!!!!看着不错
lishuichen 发表于 2024-10-29 11:18
收藏试用  感谢分享
tomliu 发表于 2024-10-29 11:21
看了源码, 思路不错, 还有图形化界面, 值得学习
haibovip 发表于 2024-10-29 11:24
感谢大佬分享。
 楼主| koogg 发表于 2024-10-29 11:35
醉生梦死. 发表于 2024-10-29 10:52
非常实用且不错的一款工具,如果能分类储存则更完美了!期待下一个版本!感谢分享!

附件更新了1.1版本,增加了你说的选择分类后,存储到对应的分类名称文件夹下,可以试试
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-12-22 19:59

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表