Python實(shí)現(xiàn)壁紙下載與輪換
準(zhǔn)備
下載安裝Python3
官網(wǎng)下載即可,選擇合適的版本:https://www.python.org/downloads/
安裝一直下一步即可,記得勾選添加到環(huán)境變量。
安裝pypiwin32
執(zhí)行設(shè)置壁紙操作需要調(diào)用Windows系統(tǒng)的API,需要安裝pypiwin32,控制臺(tái)執(zhí)行如下命令:
pip install pypiwin32
工作原理
兩個(gè)線程,一個(gè)用來(lái)下載壁紙,一個(gè)用來(lái)輪換壁紙。每個(gè)線程內(nèi)部均做定時(shí)處理,通過在配置文件中配置的等待時(shí)間來(lái)實(shí)現(xiàn)定時(shí)執(zhí)行的功能。
壁紙下載線程
簡(jiǎn)易的爬蟲工具,查詢目標(biāo)壁紙網(wǎng)站,過濾出有效連接,逐個(gè)遍歷下載壁紙。
壁紙輪換線程
遍歷存儲(chǔ)壁紙的目錄,隨機(jī)選擇一張壁紙路徑,并使用pypiwin32庫(kù)設(shè)置壁紙。
部分代碼
線程創(chuàng)建與配置文件讀取
def main():
# 加載現(xiàn)有配置文件
conf = configparser.ConfigParser()
# 讀取配置文件
conf.read("conf.ini")
# 讀取配置項(xiàng)目
search = conf.get('config', 'search')
max_page = conf.getint('config','max_page')
loop = conf.getint('config','loop')
download = conf.getint('config','download')
# 壁紙輪換線程
t1 = Thread(target=loop_wallpaper,args=(loop,))
t1.start()
# 壁紙下載線程
t2 = Thread(target=download_wallpaper,args=(max_page,search,download))
t2.start()
遍歷圖片隨機(jī)設(shè)置壁紙
def searchImage():
# 獲取壁紙路徑
imagePath = os.path.abspath(os.curdir) + '\images'
if not os.path.exists(imagePath):
os.makedirs(imagePath)
# 獲取路徑下文件
files = os.listdir(imagePath)
# 隨機(jī)生成壁紙索引
if len(files) == 0:
return
index = random.randint(0,len(files)-1)
for i in range(0,len(files)):
path = os.path.join(imagePath,files[i])
# if os.path.isfile(path):
if i == index:
if path.endswith(".jpg") or path.endswith(".bmp"):
setWallPaper(path)
else:
print("不支持該類型文件")
設(shè)置壁紙
def setWallPaper(pic): # open register regKey = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE) win32api.RegSetValueEx(regKey,"WallpaperStyle", 0, win32con.REG_SZ, "2") win32api.RegSetValueEx(regKey, "TileWallpaper", 0, win32con.REG_SZ, "0") # refresh screen win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,pic, win32con.SPIF_SENDWININICHANGE)
壁紙查詢鏈接過濾
def crawl(page,search):
# 1\. 進(jìn)入壁紙查詢頁(yè)面
hub_url = 'https://wallhaven.cc/search?q=' + search + '&sorting=random&page=' + str(page)
res = requests.get(hub_url)
html = res.text
# 2\. 獲取鏈接
## 2.1 匹配 'href'
links = re.findall(r'href=[\'"]?(.*?)[\'"\s]', html)
print('find links:', len(links))
news_links = []
## 2.2 過濾需要的鏈接
for link in links:
if not link.startswith('https://wallhaven.cc/w/'):
continue
news_links.append(link)
print('find news links:', len(news_links))
# 3\. 遍歷有效鏈接進(jìn)入詳情
for link in news_links:
html = requests.get(link).text
fing_pic_url(link, html)
print('下載成功,當(dāng)前頁(yè)碼:'+str(page));
圖片下載
def urllib_download(url):
#設(shè)置目錄下載圖片
robot = './images/'
file_name = url.split('/')[-1]
path = robot + file_name
if os.path.exists(path):
print('文件已經(jīng)存在')
else:
url=url.replace('\\','')
print(url)
r=requests.get(url,timeout=60)
r.raise_for_status()
r.encoding=r.apparent_encoding
print('準(zhǔn)備下載')
if not os.path.exists(robot):
os.makedirs(robot)
with open(path,'wb') as f:
f.write(r.content)
f.close()
print(path+' 文件保存成功')
import部分
import re import time import requests import os import configparser import random import tldextract #pip install tldextract import win32api, win32gui, win32con from threading import Thread
完整代碼請(qǐng)查看GitHub:https://github.com/codernice/wallpaper
知識(shí)點(diǎn)
- threading:多線程,這里用來(lái)創(chuàng)建壁紙下載和壁紙輪換兩個(gè)線程。
- requests:這里用get獲取頁(yè)面,并獲取最終的壁紙鏈接
- pypiwin32:訪問windows系統(tǒng)API的庫(kù),這里用來(lái)設(shè)置壁紙。
- configparser:配置文件操作,用來(lái)讀取線程等待時(shí)間和一些下載配置。
- os:文件操作,這里用來(lái)存儲(chǔ)文件,遍歷文件,獲取路徑等。
作者:華麗的碼農(nóng)
郵箱:codernice@163.com
個(gè)人博客:https://www.codernice.top
GitHub:https://github.com/codernice
以上就是Python實(shí)現(xiàn)壁紙下載與輪換的詳細(xì)內(nèi)容,更多關(guān)于python 壁紙下載與輪換的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python 下載Bing壁紙的示例
- python實(shí)現(xiàn)壁紙批量下載代碼實(shí)例
- 編寫Python腳本批量下載DesktopNexus壁紙的教程
- python批量下載壁紙的實(shí)現(xiàn)代碼
- python Requsets下載開源網(wǎng)站的代碼(帶索引 數(shù)據(jù))
- 用Python自動(dòng)下載網(wǎng)站所有文件
- python 制作網(wǎng)站小說(shuō)下載器
- python批量下載網(wǎng)站馬拉松照片的完整步驟
- python抓取網(wǎng)站的圖片并下載到本地的方法
- Python 批量下載陰陽(yáng)師網(wǎng)站壁紙
相關(guān)文章
Python之a(chǎn)scii轉(zhuǎn)中文的實(shí)現(xiàn)
這篇文章主要介紹了Python之a(chǎn)scii轉(zhuǎn)中文的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
Python Request爬取seo.chinaz.com百度權(quán)重網(wǎng)站的查詢結(jié)果過程解析
這篇文章主要介紹了Request爬取網(wǎng)站(seo.chinaz.com)百度權(quán)重的查詢結(jié)果過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
在Mac下使用python實(shí)現(xiàn)簡(jiǎn)單的目錄樹展示方法
今天小編就為大家分享一篇在Mac下使用python實(shí)現(xiàn)簡(jiǎn)單的目錄樹展示方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-11-11
使用python-cv2實(shí)現(xiàn)Harr+Adaboost人臉識(shí)別的示例
這篇文章主要介紹了使用python-cv2實(shí)現(xiàn)Harr+Adaboost人臉識(shí)別的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
python常用request庫(kù)與lxml庫(kù)操作方法整理總結(jié)
一路學(xué)習(xí),一路總結(jié),技術(shù)就是這樣,應(yīng)用之后,在進(jìn)行整理,才可以加深印象。本篇文字為小節(jié)篇,核心總結(jié) requests 庫(kù)與 lxml 庫(kù)常用的操作2021-08-08
Flask框架踩坑之a(chǎn)jax跨域請(qǐng)求實(shí)現(xiàn)
這篇文章主要介紹了Flask框架踩坑之a(chǎn)jax跨域請(qǐng)求實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2019-02-02

