每月至少凑一篇博.保持活力
这个月真个没啥好写的,帖点代码凑数.
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=0
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()
#!/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=0
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()