Use the Python script to use Bing's daily images as a desktop tutorial

Source: Internet
Author: User
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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.