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 ()