壁纸API我们使用GitHub上一个开源的Bing壁纸API作为壁纸来源https://github.com/zenghongtu/bing-wallpaper我们可以从readme中得知,在web应用中,我只需要使用下面的引用接口使用起来太方便了我们来看看API具体调用规则1、传入resolution参数,指定壁纸图片的分辨率。默认为1920x1080,可选值如下:UHD1920x12001920x10801366x7681280x7681024x768800x600800x480768x1280720x1280640x480480x8001366x7681280x768800x600800x480768x1280720x1280640x480480x800400x2403240x240HD大于Ux3202.传入index获取某一天的图片,0表示今天,1表示昨天,依此类推,index=random表示随机一天。3.传入date获取某天到今天的图片,如data=20210401。4.传入w和h指定图片的宽高。5.传入qlt指定图片的质量,取值范围0到100。比如我们直接在浏览器中输入如下地址http://bingw.jasonzeng.dev?resolutinotallow=UHD&index=random&w=1000&format=jsonOutput:{"startdate":"20220105","copyright":"板嘴山厄瓜多尔贝拉维斯塔云雾森林保护区的巨嘴鸟(?TuiDeRoy/MindenPictures)","urlbase":"/th?id=OHR.MountainToucan_EN-US7120632569","title":"Aplate-billedmountaintoucan","url":"https://www.bing.com/th?id=OHR.MountainToucan_EN-US7120632569_UHD.jpg&w=1000"}可以说是相当方便了,也可以直接在css中使用background-image:url(https://bingw.jasonzeng.dev/?index=random);height:100%;background-position:center;background-repeat:no-repeat;background-size:cover;Python调用下面看看怎么调用通过Python,也非常简单importrequestsdefget_wallpaper():foriinrange(30):url="https://bingw.jasonzeng.dev?resolutinotallow=UHD&index=%s"%str(i)print(url)res=请求。get(url)withopen("wallpaper/"+"%s.jpg"%str(i),"wb")asw:w.write(res.content)if__name__=="__main__":get_wallpaper()上面的代码是获取前30张壁纸。我们可以修改range的参数来得到不同数量的壁纸。桌面壁纸,这里使用win32con和win32gui来操作桌面壁纸defwindows_img(paper_path):k=win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Controlpanel\\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,paper_path,win32con.SPIF_SENDWININICHANGE)#刷新桌面然后从下载的壁纸中选择图片defchange_wallpaper():pic_list=os.listdir("wallpaper")#获取文件路径下的图片,列表类型i=0print(pic_list)whileTrue:pic="wallpaper"+'\{}'.format(pic_list[i])abspath_pic=os.path.abspath(pic)windows_img(abspath_pic)print(abspath_pic)time.sleep(1000)#设置壁纸更换间隔i+=1ifi==len(pic_list):#如果是最后一张图片e、回到第一个i=0if__name__=='__main__':change_wallpaper()这样一个简单的自动切换桌面壁纸的工具就完成了,快来试试吧!