Python crawler extract Bing search background image and set it to Windows PC desktop

Source: Internet
Author: User

?? 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:

    • Urllib
    • Selenium
    • Pywin32

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

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.