This article mainly introduces how to use the Python script to use Bing's daily images as a desktop tutorial. The example is implemented based on the Windows operating system environment, if you need it, you can refer to Microsoft's latest bing Bing desktop. it's good to use it. You can change the bing homepage's Beijing wallpaper every day, however, a bad thing about the software is that a search box appears on the desktop after installation, which is annoying and cannot be turned off. Therefore, for technical consideration, I thought of using Python to implement this function.
As many books have introduced in Python, Python is a glue language and is applicable wherever it is used. To use Python to set the background for the desktop, you only need to install the next module:
Http://sourceforge.net/projects/pywin32/
The code is very simple. Refer to some other people on the Internet to write the code. The specific code is as follows:
#-*-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. localbmp filename = ''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. localbmp filename = '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. localbmp filename) 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 stretch to adapt to the desktop, 0 desktop center win32api. regSetValueEx (k, "TileWallpaper", 0, win32con. REG_SZ, "0") win32gui. systemParametersInfo (win32con. SPI_SETDESKWALLPAPER, self. localbmp filename, 1 + 2) if _ name _ = '_ main _': stealBing = StealBing () stealBing. downloadImage () stealBing. updateBGImage ()