偷取Bing每天的桌面,并设置为自己的桌面,bing,Python语言: 偷取
Python语言: 偷取Bing每天的桌面,并设置为自己的桌面# -*- coding: cp936 -*-'''Created on 2009-8-17@author: lign偷取Bing的背景图片,并设置为自己的桌面背景,随后加入预览和定时偷取'''import osimport sysimport randomimport urllibimport win32guiimport win32conimport Imageclass 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:len(self.content)] tempStr = tempStr[0:tempStr.index(',id:')-1] tempStr = tempStr.replace('\\', '') self.bgImageUrl = 'http://cn.bing.com'+tempStr #仅用于生成本地文件名 def createLocalFileName(self): randomStr = ''.join(random.sample(['a','b','c','d','e','f','g','h','i','j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], 6)).replace(" ","") self.localFileName = 'c:/temp/' + randomStr + '.jpg' self.localBMPFileName = 'c:/temp/' + randomStr + '.bmp' def downloadImage(self): if self.bgImageUrl == '': self.parserImageURL() if self.localFileName == '': self.createLocalFileName() data = urllib.urlretrieve(self.bgImageUrl, self.localFileName) def updataBGImage(self): img = Image.open(self.localFileName) img.save(self.localBMPFileName) os.remove(self.localFileName) win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, self.localBMPFileName , 0)if __name__ == '__main__': stealBing = StealBing() stealBing.downloadImage() stealBing.updataBGImage()
评论关闭