?? In view of the current domestic search engine still does not use Google, the author will seek Bing search to replace. In the process of using Bing, the author found that Bing's background image is really a conscience, very pleasing, so the author's mind has a thought: can I make a crawler, you could extract the Bing search background image and set as the Windows desktop computer? The Bing search page is as follows:
?? So in a stormy afternoon, the author began his own journey of exploration. Of course, the process is tortuous, but the author tries to make it easier to speak.
?? First, we need help with some Python modules, which are:
Of course, you also need to install PHANTOMJS on your computer, because we need to crawl pages dynamically.
?? Okay, ready to work. Next is the Python code time, the complete code is as follows:
#-*-Coding:utf-8-*-"" "This program is used to extract the background picture of Bing search and set it to Windows PC desktop" "From Urllib.request import Urlretrievefrom Selenium Impo RT webdriverfrom selenium.common.exceptions Import Timeoutexceptionimport win32api,win32con,win32gui# Load Web Page browser = Webdriver with Phantomjs. PHANTOMJS () # Set the maximum wait time to 30sbrowser.set_page_load_timeout (in) url = ' https://cn.bing.com/' try:browser.get (URL) except TimeoutException: # When the load time exceeds 30 seconds, automatically stop loading the page browser.execute_script (' Window.stop () ') # Gets the information of the background picture from the tag with id bgdiv t = browser . find_element_by_id (' bgdiv ') bg = t.get_attribute (' style ') # extract the download URL of the background image from the string start_index = Bg.index (' (') End_index = Bg.index (') ') Img_url = bg[start_index+1:end_index]# Download the picture to local Img_path = ' e://bing.jpg ' Urlretrieve (Img_url, Img_path) # Set the downloaded picture to the desktop of the Windows system # Open the specified registry path Reg_key = Win32API. RegOpenKeyEx (Win32con. HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, Win32con. Key_set_value) # Final parameters: 2 extrude, 0 Center, 6 fit, 10 padding, 0 tiled win32api. RegSetValueEx (Reg_key, "Wallpaperstyle", 0, Win32con. REG_SZ, "2") # Final parameters: 1 for tiling, center for stretching, etc. are 0WIN32api. RegSetValueEx (Reg_key, "Tilewallpaper", 0, Win32con. REG_SZ, "0") # Refresh Desktop Win32gui. SystemParametersInfo (Win32con. Spi_setdeskwallpaper, Img_path, Win32con. Spif_sendwininichange)
Run this program, wait a moment, we will find that their computer wallpaper has been replaced with the background image of the Bing search page today, really is very amzing.
This share to the end, welcome to Exchange ~ ~
Python crawler extract Bing search background image and set it to Windows PC desktop