使用Python脚本将Bing的每日图片作为桌面的教程
作者:goldensun 时间:2021-02-13 22:06:42
微软最近出了个 必应bing 缤纷桌面,使用下来还是不错,可以每天更换Bing首页的北京作为壁纸,但是该软件有个不好的地方是,安装后桌面上会有一个搜索框出现,很是烦人,而且不能关掉。于是出于技术考虑,想到了使用Python来实现这个功能。
正如很多介绍Python书中那样,Python是中胶水语言,用在哪里都是可行的。想要使用Python给桌面设置背景只需要下个模块安装即可:
http://sourceforge.net/projects/pywin32/
代码非常简单,参考了网上一些其他人写了代码,具体代码如下:
# -*- coding: utf-8 -*-
import urllib,time,os,Image,win32gui,win32con,win32api
class StealBing:
def __init__(self):
self.content = urllib.urlopen('http://cn.bing.com/').read()
self.bgImageUrl = ''
self.localFileName = ''
self.localBMPFileName = ''
def parserImageURL(self):
tempStr = self.content[self.content.index('g_img={url:')+12:]
self.bgImageUrl = tempStr[:tempStr.index('id:\'bgDiv\'')-2]
def createLocalFileName(self):
randomStr = time.strftime("%Y%m%d", time.localtime())
self.localFileName = 'D:/Bing/' + randomStr + '.jpg'
self.localBMPFileName = 'D:/Bing/' + randomStr + '.bmp'
def downloadImage(self):
if self.bgImageUrl == '':
self.parserImageURL()
if self.localFileName == '':
self.createLocalFileName()
urllib.urlretrieve(self.bgImageUrl, self.localFileName)
def updateBGImage(self):
img = Image.open(self.localFileName)
img.save(self.localBMPFileName)
os.remove(self.localFileName)
k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2") #2拉伸适应桌面,0桌面居中
win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, self.localBMPFileName , 1+2)
if __name__ == '__main__':
stealBing = StealBing()
stealBing.downloadImage()
stealBing.updateBGImage()
标签:Python
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL Basis 常用命令
2010-11-11 11:59:00
如何“看看”服务器上的那个文件还在不在?
2009-11-02 20:17:00
js版sliderBar(滑动条)控件
2008-10-18 15:59:00
低效的键盘和高效的登录框
2007-08-22 09:17:00
![](https://img.aspxhome.com/file/UploadPic/20078/22/200782291926838s.jpg)
不唐突的JavaScript的七条准则[翻译]
2008-12-09 13:33:00
显示/隐藏引出的CSS Bug
2010-10-20 20:13:00
Oracle row_number() over()解析函数高效实现分页
2011-12-01 10:33:07
PHP简易延时队列的实现流程详解
2023-05-29 23:02:48
SQL 统计一个数据库中所有表记录的数量
2012-01-29 18:21:36
asp,php,.net使用301重定向方法
2007-09-26 14:05:00
PHP文件运行步骤以及数据库的连接代码
2023-05-25 08:32:51
Access命令行参数
2007-08-23 15:35:00
js用星投票的示例代码
2007-12-29 13:11:00
![](https://img.aspxhome.com/file/UploadPic/200712/29/20071229131618418s.gif)
初学MySql5 所应了解的知识和常见问题
2009-03-06 17:49:00
Select的OnChange()事件
2009-03-03 12:40:00
PHP程序员玩转Linux系列 备份还原MySQL
2023-11-15 07:39:28
![](https://img.aspxhome.com/file/2023/7/67207_0s.png)
CSS实现完美垂直居中
2007-09-22 09:29:00
PHP编程实现多维数组按照某个键值排序的方法小结【2种方法】
2023-09-11 08:28:26
![](https://img.aspxhome.com/file/2023/7/55317_0s.jpg)
asp任何取得多个表单的值
2008-04-15 15:31:00
Javascript DOM 编程实例讲解--仿LightBox效果提示框
2008-05-01 13:25:00
![](https://img.aspxhome.com/file/UploadPic/up/2008050116095611.gif)