一键设置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)

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)