前言
發(fā)現(xiàn)一個(gè)不錯(cuò)的壁紙網(wǎng)站,里面都是超高清的圖片,而且還是免費(fèi)為的。
所以,我打算把這些壁紙都爬取下來,然后在做一個(gè)自動(dòng)跟換桌面壁紙的腳本,這樣基本上你一年都可以每天都有不重復(fù)桌面了
目標(biāo)地址
先來看看我們這次的受害者:https://wallhaven.cc/
【付費(fèi)VIP完整版】只要看了就能學(xué)會(huì)的教程,80集Python基礎(chǔ)入門視頻教學(xué)
點(diǎn)這里即可免費(fèi)在線觀看
先是爬蟲代碼
導(dǎo)入數(shù)據(jù)
import requests import re
請(qǐng)求數(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] 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)
運(yùn)行代碼,查看結(jié)果
自動(dòng)跟換桌面壁紙代碼
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('請(qǐng)輸入文件路徑:') 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('請(qǐng)輸入文件路徑:') 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()
最后實(shí)現(xiàn)效果
到此這篇關(guān)于趣味Python實(shí)戰(zhàn)練習(xí)之自動(dòng)更換桌面壁紙腳本附源碼的文章就介紹到這了,更多相關(guān)Python 自動(dòng)更換壁紙內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://pythonjx.blog.csdn.net/article/details/120705122