使用Python获取当前Bing的背景图片并设置为Windows壁纸
2022/3/29 7:27:53
本文主要是介绍使用Python获取当前Bing的背景图片并设置为Windows壁纸,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
下面的代码在我写此博客时编辑通过,很简单,如果Bing的背景图片格式发生变化,请修改对应的正则匹配代码即可。
#------------------------------------------------------------------------------- # Name: BingWallPaper # Purpose: # # Author: xxh # # Created: 28-03-2022 # Copyright: (c) xxh 2022 # Licence: <your licence> #------------------------------------------------------------------------------- import requests import re import win32gui import os import datetime,time from PIL import Image # 设置壁纸 def setWallpaper(imgPath): win32gui.SystemParametersInfo(20, imgPath, 3) # 下载图片 def downImg(IMAGE_URL): # 图片以当天日期命名,这里默认为当前路径,实际使用时可以指定一个壁纸存放的专门路径 fileName = time.strftime("%Y-%m-%d", time.localtime()) +'.jpg' r = requests.get(IMAGE_URL) with open(fileName, 'wb') as f: f.write(r.content) return fileName def main(): res = requests.get('https://cn.bing.com/') res.encoding = 'utf-8' # res.text为网页源码,这里要用到的是图片 # <link rel="preload" href="https://s.cn.bing.net/th?id=OHR.Kawachi_ZH-CN6964965791_1920x1080.jpg&rf=LaDigue_1920x1080.jpg" as="image" id="preloadBg" /> pattern = re.compile(r'href="(https://s\.cn\.bing\.net/th\?id=.*\.jpg)" as=') result = pattern.findall(res.text) # result[0]:https://s.cn.bing.net/th?id=OHR.Kawachi_ZH-CN6964965791_1920x1080.jpg&rf=LaDigue_1920x1080.jpg fileName = downImg(result[0]) # 转换文件格式为bmp im = Image.open(fileName) im.convert('RGB').save(fileName[:-3]+'bmp','BMP') # 设置壁纸 setWallpaper(os.getcwd()+'/'+fileName[:-3]+'bmp') if __name__ == '__main__': main()
这篇关于使用Python获取当前Bing的背景图片并设置为Windows壁纸的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python