python實(shí)現(xiàn)桌面壁紙切換功能
本文實(shí)例為大家分享了python實(shí)現(xiàn)桌面壁紙切換功能的具體實(shí)現(xiàn)方法,供大家參考,具體內(nèi)容如下
大體分為兩個(gè)部分
一、利用爬蟲爬取壁紙
第一部分爬取圖片url地址并且下載至本地
爬蟲針對(duì) http://image.so.com/ 【360壁紙寫的】,如果要更換url地址自己改改
import requests
import json
import random
import os
#存放Ajax圖片地址數(shù)據(jù)
img_url_dict={}
#創(chuàng)建圖片tmp文件夾
if not os.path.exists('image'):
os.mkdir('image')
#爬取圖片url地址
def getImgurl(root_url,sn):
params={
'ch': 'wallpaper',
't1': 157,
'sn': sn,
'listtype': 'new',
'temp': 1
}
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit / 537.36(KHTML, like Gecko)Chrome/62.0 3202.62 Safari / 537.36'
}
try:
response=requests.get(root_url,params=params,headers=headers)
except RequestException:
return None
data=json.loads(response.text).get('list')
img_url_list=[]
for item in data:
img_url_list.append(item.get('cover_imgurl'))
img_url_dict[sn]=img_url_list
#下載圖片
def download_image(name,image_url):
try:
response=requests.get(image_url)
except RequestException:
return "圖像請(qǐng)求出錯(cuò)"
file_name='{}/{}.{}'.format('image',name,'bmp');
with open(file_name,'wb') as file:
file.write(response.content)
#獲取隨機(jī)url地址并下載至image文件夾
def get_img():
sn=30*random.randint(1,15)
try:
img_url_dict[sn]
except KeyError:
getImgurl('http://image.so.com/zj',sn)
index=random.randint(0,len(img_url_dict[sn])-1)
url=img_url_dict[sn][index]
download_image('wallpaper',url)
二、更換桌面壁紙
第二部分將下載的圖片作為壁紙,間隔一定時(shí)間重新下載,再切換壁紙
這部分借用python實(shí)現(xiàn)windows壁紙定期更換功能
import win32api, win32gui, win32con import time 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)
if __name__=='__main__':
while True:
get_img()
pic='your_path/image/wallpaper.bmp'#寫絕對(duì)路徑
setWallPaper(pic)
time.sleep(6)#6s切換一次壁紙
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python開發(fā)SQLite3數(shù)據(jù)庫相關(guān)操作詳解【連接,查詢,插入,更新,刪除,關(guān)閉等】
這篇文章主要介紹了Python開發(fā)SQLite3數(shù)據(jù)庫相關(guān)操作,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python操作SQLite3數(shù)據(jù)庫的連接,查詢,插入,更新,刪除,關(guān)閉等相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Python實(shí)現(xiàn)輸出某區(qū)間范圍內(nèi)全部素?cái)?shù)的方法
這篇文章主要介紹了Python實(shí)現(xiàn)輸出某區(qū)間范圍內(nèi)全部素?cái)?shù)的方法,涉及Python數(shù)值運(yùn)算、排序、判斷等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
python實(shí)現(xiàn)密度聚類(模板代碼+sklearn代碼)
這篇文章主要介紹了python實(shí)現(xiàn)密度聚類(模板代碼+sklearn代碼),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
python如何利用cv2.rectangle()繪制矩形框
cv2.rectangle這個(gè)函數(shù)的作用是在圖像上繪制一個(gè)簡(jiǎn)單的矩形,下面這篇文章主要給大家介紹了關(guān)于python如何利用cv2.rectangle()繪制矩形框的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12

