【Python实用小妙招】用python采集数万张桌面壁纸,再也不用担心找不到高质量壁纸

嗨嗨,大家好,这里是小圆 ~

不知道你们是不是这样的:每次壁纸用一段时间就会要换下一张
但是吧,每张壁纸用的时间也不长,每次找壁纸都要找很久,有的有水印,有的就是清晰度不高

嘿嘿,所以~今天给大家带来福利咯直接用Python采集高质量的壁纸

请添加图片描述

先来看看我们这次的受害者 : 不知道这个能放出来不

https://wallhaven.cc/

先获取图片

导入模块

源码.资料点击领取

import requests
import re

请求数据

for page in range(1, 126):
    url = 'https://wallhaven.cc/toplist?page={}'.format(page)
    headers = {
    
    
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
    }
    response = requests.get(url=url, headers=headers)

解析数据

urls = re.findall('<a class="preview" href="(.*?)"', response.text)
for i in urls:
    response_2 = requests.get(url=i, headers=headers)
    img_url = re.findall('<img id="wallpaper" src="(.*?)"', response_2.text)[0]
    title = img_url.split('-')[-1]
    download(title, img_url)
    print(img_url)

保存数据

def download(title, url):
    path = 'img\\' + title
    response = requests.get(url=url)
    with open(path, mode='wb') as f:
        f.write(response.content)

部分效果

在这里插入图片描述

在这里插入图片描述

自动更换桌面壁纸

def Windows_img(paperPath):
    k=win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control panel\\Desktop",0,win32con.KEY_SET_VALUE)
    # 在注册表中写入属性值
    win32api.RegSetValueEx(k,"wapaperStyle",0,win32con.REG_SZ,"2")  # 0 代表桌面居中 2 代表拉伸桌面
    win32api.RegSetValueEx(k,"Tilewallpaper",0,win32con.REG_SZ,"0")
    win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,paperPath,win32con.SPIF_SENDWININICHANGE) # 刷新桌面

def changeWallpaper():
    """文件夹/文件夹/图片"""
    path=input('请输入文件路径:')
    L2=os.listdir(path=path)  # 得到文件路径下的壁纸文件夹,列表类型
    i=0
    print(L2)   # 壁纸文件夹
    url_list = []
    for l2 in L2:
        detail_path = path + '\\' + l2
        L3 = os.listdir(detail_path)    # 得到壁纸文件夹路径下的图片,列表类型
        for l3 in L3:
            url_list.append(detail_path + '\\' + l3)
    print(url_list)
    while True:
        Windows_img(url_list[i])
        print('{}'.format(url_list[i]))
        time.sleep(2)  # 设置壁纸更换间隔,这里为10秒,根据用户自身需要自己设置秒数
        i += 1
        if i == len(url_list):  # 如果是最后一张图片,则重新到第一张
            i = 0
python学习交流Q群:770699889 ###
def changeWallpaper_2():
    """文件夹/图片"""
    path=input('请输入文件路径:')
    L2=os.listdir(path=path)  # 得到文件路径下的图片,列表类型
    i=0
    print(L2)
    while True:
        Windows_img(path+'\{}'.format(L2[i]))
        print(path+'\{}'.format(L2[i]))
        time.sleep(1000)  # 设置壁纸更换间隔,这里为10秒,根据用户自身需要自己设置秒数
        i += 1
        if i==len(L2):  # 如果是最后一张图片,则重新到第一张
            i=0
if __name__ == '__main__':
    changeWallpaper()

展示一下

请添加图片描述

好啦,今天的分享到这里就结束了 ~
对文章有问题的,或者有其他关于python的问题,可以在评论区留言或者私信我哦
觉得我分享的文章不错的话,可以关注一下我,或者给文章点赞(/≧▽≦)/

请添加图片描述

猜你喜欢

转载自blog.csdn.net/aliYz/article/details/127127062