一键设置Bing搜索背景设为windows桌面壁纸脚本 配置完脚本再设置windows定时任务即可每天更换桌面背景
import requests import json import sys from datetime import datetime import os import win32api,win32con,win32gui url = 'https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&nc=1603413047356&pid=hp&FORM=Z9FD1' res = requests.get(url) res_json = json.loads(res.text) # print(res_json['images'][0]['url']) image_name = res_json['images'][0]['copyright'].split('(')[0] print(image_name) #获取图片地址 根据地址拼接url bing_url = 'https://cn.bing.com{}'.format(res_json['images'][0]['url']) # print(bing_url) image_res = requests.get(bing_url) basePath = sys.path[0] # print(os.path.dirname(basePath)) #当前路径是否为文件 是文件则取父目录 if (os.path.isfile(basePath)): basePath = os.path.dirname(basePath) baseFoler = basePath + '\Download\' #创建Download文件夹 if(not os.path.exists(baseFoler)): os.makedirs(baseFoler) jpgfile = basePath + '\' +image_name jpgfile = baseFoler + image_name + str(datetime.now().strftime('%Y-%m-%d'))+'.jpg' print(jpgfile) #保存图片 with open(jpgfile,'wb') as file: file.write(image_res.content) print(u'正在设置图片:%s为桌面壁纸...' % image_name) key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\Desktop",0,win32con.KEY_SET_VALUE ) win32api.RegSetValueEx(key,"WallpaperStyle",0,win32con.REG_SZ,"2") #2拉伸适应桌面,0桌面居中 win32api.RegSetValueEx(key,"TileWallpaper",0,win32con.REG_SZ,"0") win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,jpgfile,1+2) print(u'成功应用图片:%s为桌面壁纸' % image_name)