程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] collect 30000 4K ultra clear wallpapers, and realize the script of regularly and automatically changing desktop wallpapers (including complete source code)

編輯:Python

Preface

Hi. ! Hello everyone , This is the devil ~

Find a good wallpaper website , It's full of Ultra HD pictures

therefore , I'm going to collect all these wallpaper , Then I'm making a script to automatically change the desktop wallpaper , In this way, you can basically have a non repetitive desktop every day for a year

Let's take a look at our victims this time : https://wallhaven.cc/

It's a website with very high picture quality

First, get the picture

The import module

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)

Parsing 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 the data

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

effect


Python From zero basic introduction to actual combat system tutorial 、 Source code 、 video , Students who want data sets can also click here

Automatically change desktop wallpaper

def Windows_img(paperPath):
k=win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control panel\\Desktop",0,win32con.KEY_SET_VALUE)
# Write attribute values in the registry
win32api.RegSetValueEx(k,"wapaperStyle",0,win32con.REG_SZ,"2") # 0 Represents the center of the desktop 2 Stands for stretched desktop
win32api.RegSetValueEx(k,"Tilewallpaper",0,win32con.REG_SZ,"0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,paperPath,win32con.SPIF_SENDWININICHANGE) # Refresh the desktop
def changeWallpaper():
""" Folder / Folder / picture """
path=input(' Please enter the file path :')
L2=os.listdir(path=path) # Get the wallpaper folder under the file path , List the type
i=0
print(L2) # Wallpaper folder
url_list = []
for l2 in L2:
detail_path = path + '\\' + l2
L3 = os.listdir(detail_path) # Get the picture under the wallpaper folder path , List the type
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) # Set wallpaper replacement interval , Here for 10 second , Set the number of seconds according to the user's own needs
i += 1
if i == len(url_list): # If it's the last picture , Then go back to the first one
i = 0
def changeWallpaper_2():
""" Folder / picture """
path=input(' Please enter the file path :')
L2=os.listdir(path=path) # Get the picture under the file path , List the type
i=0
print(L2)
while True:
Windows_img(path+'\{}'.format(L2[i]))
print(path+'\{}'.format(L2[i]))
time.sleep(1000) # Set wallpaper replacement interval , Here for 10 second , Set the number of seconds according to the user's own needs
i += 1
if i==len(L2): # If it's the last picture , Then go back to the first one
i=0
if __name__ == '__main__':
changeWallpaper()

Tail language

Okay , My article ends here !

There are more suggestions or questions to comment on or send me a private letter ! Come on together and work hard (ง •_•)ง

If you like, just pay attention to the blogger , Or like the collection and comment on my article !!!


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved