Share a website and use Python to collect 4K wallpapers in batches [Python Collection Wallpaper Series 1]

foreword

Start a new series, share some websites with you, you can collect some high-definition watermark-free wallpapers

Hurry up while you're still here.

insert image description here

put a URL

https://wallhaven.cc/

get picture

import module

import requests
import re

request data

python学习交流Q裙 770699889###
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)

Show some effects

insert image description here

insert image description here
insert image description here

Change wallpaper automatically

Source code. Material. Information. Click to get it

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) # 刷新桌面

insert image description here
insert image description here
insert image description here

Show all effects

insert image description here
Alright, that's the end of today's sharing

Immediately write the next article, and then share the second wallpaper website~

Here I will share with you an introductory tutorial on Python zero foundation

The most suitable collection of video tutorials for novice Xiaobai to learn python [Python zero-based introductory tutorial]

insert image description here

Guess you like

Origin blog.csdn.net/Dangerous_li/article/details/127561518