zoukankan      html  css  js  c++  java
  • 每月至少凑一篇博.保持活力

    这个月真个没啥好写的,帖点代码凑数.
    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()
  • 相关阅读:
    Linux 下 的 Oracle,如何安装 tnsname
    checkpoint process vs writer process vs wal writer process
    PostgreSQL的checkpoint能否并行
    PostgreSQL参数学习:wal_keep_segments
    PostgreSQL参数学习:vacuum_defer_clean_age
    PostgreSQL参数学习:max_wal_senders
    对PostgreSQL数据库的hstore类型建立GisT索引的实验
    PostgreSQL的hstore初步学习
    乐鑫esp8266基于freeRtos实现私有服务器本地远程OTA升级
    Android 6.0 超级简单的权限申请2 (Permission)
  • 原文地址:https://www.cnblogs.com/solo/p/1588811.html
Copyright © 2011-2022 走看看