亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Python實現(xiàn)壁紙下載與輪換

瀏覽:11日期:2022-07-07 17:41:33

準備

下載安裝Python3

官網(wǎng)下載即可,選擇合適的版本:https://www.python.org/downloads/安裝一直下一步即可,記得勾選添加到環(huán)境變量。

安裝pypiwin32

執(zhí)行設(shè)置壁紙操作需要調(diào)用Windows系統(tǒng)的API,需要安裝pypiwin32,控制臺執(zhí)行如下命令:

pip install pypiwin32

工作原理

兩個線程,一個用來下載壁紙,一個用來輪換壁紙。每個線程內(nèi)部均做定時處理,通過在配置文件中配置的等待時間來實現(xiàn)定時執(zhí)行的功能。

壁紙下載線程

簡易的爬蟲工具,查詢目標壁紙網(wǎng)站,過濾出有效連接,逐個遍歷下載壁紙。

壁紙輪換線程

遍歷存儲壁紙的目錄,隨機選擇一張壁紙路徑,并使用pypiwin32庫設(shè)置壁紙。

部分代碼

線程創(chuàng)建與配置文件讀取

def main(): # 加載現(xiàn)有配置文件 conf = configparser.ConfigParser() # 讀取配置文件 conf.read('conf.ini') # 讀取配置項目 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()

遍歷圖片隨機設(shè)置壁紙

def searchImage(): # 獲取壁紙路徑 imagePath = os.path.abspath(os.curdir) + ’images’ if not os.path.exists(imagePath): os.makedirs(imagePath) # 獲取路徑下文件 files = os.listdir(imagePath) # 隨機生成壁紙索引 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 PanelDesktop',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. 進入壁紙查詢頁面 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. 遍歷有效鏈接進入詳情 for link in news_links: html = requests.get(link).text fing_pic_url(link, html) print(’下載成功,當前頁碼:’+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(’準備下載’) 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 reimport timeimport requestsimport osimport configparserimport randomimport tldextract #pip install tldextractimport win32api, win32gui, win32confrom threading import Thread

完整代碼請查看GitHub:https://github.com/codernice/wallpaper

知識點

threading:多線程,這里用來創(chuàng)建壁紙下載和壁紙輪換兩個線程。 requests:這里用get獲取頁面,并獲取最終的壁紙鏈接 pypiwin32:訪問windows系統(tǒng)API的庫,這里用來設(shè)置壁紙。 configparser:配置文件操作,用來讀取線程等待時間和一些下載配置。 os:文件操作,這里用來存儲文件,遍歷文件,獲取路徑等。

作者:華麗的碼農(nóng)郵箱:[email protected]個人博客:https://www.codernice.topGitHub:https://github.com/codernice

以上就是Python實現(xiàn)壁紙下載與輪換的詳細內(nèi)容,更多關(guān)于python 壁紙下載與輪換的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 欧美亚洲国产一级毛片 | 男女啪视频大全1000 | 久久精品国产亚洲香蕉 | 久久精品国产2020 | 久久精品免费一区二区视 | 成人mv高清在线 | 在线观看麻豆国产精品 | 国产丝袜第一页 | 男女晚上日日麻批视频不挡 | 最新内地三级在线观看 | 在线视频观看国产 | 久久精品中文字幕有码日本 | 亚洲国产高清一区二区三区 | 欧美换爱交换乱理伦片免费观看 | 精品哟哟哟国产在线观看不卡 | 日本高清精品 | 欧美巨大另类极品videohd | 无毒不卡 | 成年做羞羞免费观看视频网站 | 日本人一级毛片视频 | 泰国一级淫片免费看 | 男人天堂综合网 | 小明免费| 久久dvd| 亚洲第一黄 | 精品一区二区三区在线观看l | 五月天婷婷网亚洲综合在线 | 99久久久久国产精品免费 | 韩国日本一级片 | 久久er热这里只有精品免费 | 日韩3级| 91新视频 | 性视频免费视频大全 | 国产亚洲综合在线 | www.爱色| 波多野结衣黑人系列在线观看 | 国产欧美一区二区精品性色99 | 情趣色视频网站 | 国产丝袜护土调教在线视频 | 国产亚洲精品久久久久久久网站 | 成人午夜网 |