趣味Python實戰(zhàn)練習(xí)之自動更換桌面壁紙腳本附源碼
發(fā)現(xiàn)一個不錯的壁紙網(wǎng)站,里面都是超高清的圖片,而且還是免費為的。
所以,我打算把這些壁紙都爬取下來,然后在做一個自動跟換桌面壁紙的腳本,這樣基本上你一年都可以每天都有不重復(fù)桌面了
目標(biāo)地址
先來看看我們這次的受害者:https://wallhaven.cc/


【付費VIP完整版】只要看了就能學(xué)會的教程,80集Python基礎(chǔ)入門視頻教學(xué)
點這里即可免費在線觀看
先是爬蟲代碼
導(dǎo)入數(shù)據(jù)
import requests import re
請求數(shù)據(jù)
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)
解析數(shù)據(jù)
urls = re.findall('<a class="preview" href="(.*?)" rel="external nofollow" ', 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)
保存數(shù)據(jù)
def download(title, url): path = 'img\\' + title response = requests.get(url=url) with open(path, mode='wb') as f: f.write(response.content)
運行代碼,查看結(jié)果



自動跟換桌面壁紙代碼
import win32api
import win32con
import win32gui
import os
import time
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) # 設(shè)置壁紙更換間隔,這里為10秒,根據(jù)用戶自身需要自己設(shè)置秒數(shù)
i += 1
if i == len(url_list): # 如果是最后一張圖片,則重新到第一張
i = 0
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) # 設(shè)置壁紙更換間隔,這里為10秒,根據(jù)用戶自身需要自己設(shè)置秒數(shù)
i += 1
if i==len(L2): # 如果是最后一張圖片,則重新到第一張
i=0
if __name__ == '__main__':
changeWallpaper()
最后實現(xiàn)效果

到此這篇關(guān)于趣味Python實戰(zhàn)練習(xí)之自動更換桌面壁紙腳本附源碼的文章就介紹到這了,更多相關(guān)Python 自動更換壁紙內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信