[Python practical tips] Use python to collect tens of thousands of desktop wallpapers, no longer have to worry about finding high-quality wallpapers

Hi everyone, this is Miao Yuan~

I don’t know if you are like this: every time the wallpaper is used for a period of time, the next one will be replaced
. But, each wallpaper does not take long, and it takes a long time to find a wallpaper every time, some have watermarks, some have watermarks is not clear

Hehe, so~ I'll bring you some benefits today.Collect high-quality wallpapers directly with Python

Please add image description

Let's take a look at our victims this time: I don't know if this can be released or not.

https://wallhaven.cc/

Get the picture first

import module

Source code. Information click to receive

import requests
import re

request data

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)

Analytical data

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)

save data

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

partial effect

insert image description here

insert image description here

Automatically change desktop wallpaper

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()

show it

Please add image description

Well, today's sharing is over here~ If you
have any questions about the article, or have other questions about python, you can leave a message in the comment area or privately message me . If you
think the article I shared is good, you can follow me, or give Article like (/≧▽≦)/

Please add image description

Guess you like

Origin blog.csdn.net/aliYz/article/details/127127062