Create at least one blog every month to maintain vitality.

Source: Internet
Author: User

This month is really nothing to write, post pointCodeNumber.
Pyhon downloads the background image of Bing. cn homepage and sets it to desktop. (I put it in "start" and change the background every day when I start it)

 

Python3.0
# ! /Usr/bin/ENV Python
# Coding = UTF-8
# Author: haozes
# Download the background image on the Bing. cn homepage and set it to a desktop background image.
Import Urllib. Request
Import Re
Import OS. Path

FromWin32com. ShellImportShell
ImportPythoncom
ImportSys
ImportTime

Def Getfilename (URL ):
If URL = None: Return None
If URL = "" : Return   ""
Arr = URL. Split ( " / " )
Return Arr [Len (ARR) - 1 ]

Def Setdskimgfrombing ():
URL = " Http://cn.bing.com "
Savepath = R " C: \ Bing \ img "
Response = Urllib. Request. urlopen (URL)
Html = Response. Read (). Decode ( ' UTF-8 ' )
Reobj = Re. Compile (R " G_img =\{ URL: '(. + ?) ', ID: 'bgdiv' " , Re. dotall | Re. ignorecase)
Match = Reobj. Search (HTML)
If Match:
Result = Match. Group ( 1 )
Else :
Result =   ""
Bgpicurl = URL + Result. Replace ( ' \\ ' , '' );
Print (Bgpicurl)
Filename = Getfilename (bgpicurl );
Print ( " Down PIC: " + Filename)
Dstpath = Savepath + " \\ " + Filename
If OS. Path. exists (dstpath ):
Print ( " IMG file exist! " )
Return
If OS. Path. exists (savepath) = False:
OS. mkdir (savepath)
Urllib. Request. urlretrieve (bgpicurl, dstpath)
Time. Sleep ( 1 )
If Setwallpaper (dstpath ):
Print ( " Set desktop OK! " )
Else :
Print ( " Set desktop failed! " )
Pass

Def Setwallpaper (wallpaper, Style = 2 ):
Try :
IAD = Pythoncom. cocreateinstance (shell. clsid_activedesktop, none, pythoncom. clsctx_inproc_server, Shell. iid_iactivedesktop)
Opts = IAD. getasktopitemoptions ()
Reserved = 0
IAD. setwallpaper (wallpaper, reserved)
IAD. setwallpaperoptions (style, reserved)
IAD. applychanges ( 0 xFFFF )
Except :
Print ( " Unexpected error: " , SYS. exc_info () [0])
Return False
Else :
Return True


If   _ Name __   =   " _ Main __ " :
Setdskimgfrombing ()

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.