• 每月至少凑一篇博.保持活力


    这个月真个没啥好写的,帖点代码凑数.
    pyhon 下载BING.CN首页背景图片,并设置为桌面.(我放在"启动"里,每天开机换一个背景)

    python3.0
    #!/usr/bin/env python
    #
    coding=utf-8
    #
    author:haozes
    #
    下载BING.CN首页上的背景图片,并设置为桌面背景图片
    import urllib.request
    import re
    import os.path

    from win32com.shell import shell
    import pythoncom
    import sys
    import time


    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.GetDesktopItemOptions()  
           Reserved
    =
           iad.SetWallpaper(wallpaper, Reserved)  
           iad.SetWallpaperOptions(style,Reserved)  
           iad.ApplyChanges(
    0xffff)  
        
    except:
            
    print("Unexpected error:", sys.exc_info()[0])
            
    return False 
        
    else:  
           
    return True 
       
        
    if __name__ == "__main__":
        setDskImgFromBing()
  • 相关阅读:
    iOS开发之直接使用UISearchBar
    iOS开发之cell多按钮
    Cocoapods报错xcrun: error: active developer path ("/Users/wangwei/Downloads/Xcode.app/Contents/Developer") does not exist
    lyl
    最全ASCLL码
    在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法
    ANSI escape code
    cocoa
    boundingRectWithSize:options:attributes:context:
    iOS 9 storyboard自动布局
  • 原文地址:https://www.cnblogs.com/solo/p/1588811.html
Copyright © 2020-2023  润新知