python设置windows桌面壁纸的实现代码
#-*-coding:UTF-8-*-
from__future__importunicode_literals
importImage
importdatetime
importwin32gui,win32con,win32api
importre
fromHttpWrapperimportSendRequest
StoreFolder="c:\\dayImage"
defsetWallpaperFromBMP(imagepath):
k=win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"ControlPanel\\Desktop",0,win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(k,"WallpaperStyle",0,win32con.REG_SZ,"2")#2拉伸适应桌面,0桌面居中
win32api.RegSetValueEx(k,"TileWallpaper",0,win32con.REG_SZ,"0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath,1+2)
defsetWallPaper(imagePath):
"""
Givenapathtoanimage,convertittobmpandsetitaswallpaper
"""
bmpImage=Image.open(imagePath)
newPath=StoreFolder+"\\mywallpaper.bmp"
bmpImage.save(newPath,"BMP")
setWallpaperFromBMP(newPath)
defgetPicture():
url="http://photography.nationalgeographic.com/photography/photo-of-the-day/"
h=SendRequest(url)
ifh.GetSource():
r=re.findall("<divclass="download_link"><ahref="(.*?)">Download",h.GetSource())
ifr:
returnSendRequest(r[0]).GetSource()
else:
print"解析图片地址出错,请检查正则表达式是否正确"
returnNone
defsetWallpaperOfToday():
img=getPicture()
ifimg:
path=StoreFolder+"\\%s.jpg"%datetime.date.today()
f=open(path,"wb")
f.write(img)
f.close()
setWallPaper(path)
setWallpaperOfToday()
print"Wallpapersetok!"
其中的httpwrapper是我写的一个http访问的封装:
#!/usr/bin/envpython
#-*-coding:UTF-8-*-
#-------------------------------------------------------------------------------
#Name:对http访问的封装
#
#Author:qianlifeng
#
#Created:10-02-2012
#-------------------------------------------------------------------------------
importbase64
importurllib
importurllib2
importtime
importre
importsys
classSendRequest:
"""
网页请求增强类
SendRequest("http://xxx.com",data=dict,type="POST",auth="base",user="xxx",password="xxx")
"""
def__init__(self,url,data=None,method="GET",auth=None,user=None,password=None,cookie=None,**header):
"""
url:请求的url,不能为空
date:需要post的内容,必须是字典
method:Get或者Post,默认为Get
auth:"base"或者"cookie"
user:用于base认证的用户名
password:用于base认证的密码
cookie:请求附带的cookie,一般用于登录后的认证
其他头信息:
e.g.referer="www.sina.com.cn"
"""
self.url=url
self.data=data
self.method=method
self.auth=auth
self.user=user
self.password=password
self.cookie=cookie
if"referer"inheader:
self.referer=header[referer]
else:
self.referer=None
if"user-agent"inheader:
self.user_agent=header[user-agent]
else:
##self.user_agent="Mozilla/5.0(WindowsNT5.1;rv:8.0)Gecko/20100101Firefox/8.0"
self.user_agent="Mozilla/5.0(iPhone;U;CPUiPhoneOS3_0likeMacOSX;en-us)AppleWebKit/528.18(KHTML,likeGecko)Version/4.0Mobile/7A341Safari/528.16"
self.__SetupRequest()
self.__SendRequest()
def__SetupRequest(self):
ifself.urlisNoneorself.url=="":
raise"url不能为空!"
#访问方式设置
ifself.method.lower()=="post":
self.Req=urllib2.Request(self.url,urllib.urlencode(self.data))
elifself.method.lower()=="get":
ifself.data==None:
self.Req=urllib2.Request(self.url)
else:
self.Req=urllib2.Request(self.url+"?"+urllib.urlencode(self.data))
#设置认证信息
ifself.auth=="base":
ifself.user==Noneorself.password==None:
raise"Theuserorpasswordwasnotgiven!"
else:
auth_info=base64.encodestring(self.user+":"+self.password).replace("\n","")
auth_info="Basic"+auth_info
self.Req.add_header("Authorization",auth_info)
elifself.auth=="cookie":
ifself.cookie==None:
raise"Thecookiewasnotgiven!"
else:
self.Req.add_header("Cookie",self.cookie)
ifself.referer:
self.Req.add_header("referer",self.referer)
ifself.user_agent:
self.Req.add_header("user-agent",self.user_agent)
def__SendRequest(self):
try:
self.Res=urllib2.urlopen(self.Req)
self.source=self.Res.read()
self.code=self.Res.getcode()
self.head_dict=self.Res.info().dict
self.Res.close()
except:
print"Error:HttpWrapper=>_SendRequest",sys.exc_info()[1]
defGetResponseCode(self):
"""
得到服务器返回的状态码(200表示成功,404网页不存在)
"""
returnself.code
defGetSource(self):
"""
得到网页源代码,需要解码后在使用
"""
if"source"indir(self):
returnself.source
returnu""
defGetHeaderInfo(self):
"""
u"得到响应头信息"
"""
returnself.head_dict
defGetCookie(self):
"""
得到服务器返回的Cookie,一般用于登录后续操作
"""
if"set-cookie"inself.head_dict:
returnself.head_dict["set-cookie"]
else:
returnNone
defGetContentType(self):
"""
得到返回类型
"""
if"content-type"inself.head_dict:
returnself.head_dict["content-type"]
else:
returnNone
defGetCharset(self):
"""
尝试得到网页的编码
如果得不到返回None
"""
contentType=self.GetContentType()
ifcontentTypeisnotNone:
index=contentType.find("charset")
ifindex>0:
returncontentType[index+8:]
returnNone
defGetExpiresTime(self):
"""
得到网页过期时间
"""
if"expires"inself.head_dict:
returnself.head_dict["expires"]
else:
returnNone
defGetServerName(self):
"""
得到服务器名字
"""
if"server"inself.head_dict:
returnself.head_dict["server"]
else:
returnNone
__all__=[SendRequest,]
if__name__=="__main__":
b=SendRequest("http://www.baidu.com")
printb.GetSource()
相关文章
- python学习笔记(三)— PyCharm 下载安装教程(Windows)
- Python常用代码_python画图代码大全
- python用pip安装whl文件
- 分分钟教你用python爬了一万多张小姐姐壁纸
- 【说站】Python中GC算法是什么
- 经纬度距离计算 python_Python已知两坐标求距离
- python分析人口出生率代码_国家统计局居然也能用的上Python?人口数据Python脚本了解一下?…[通俗易懂]
- VSCode 最好的 Python 扩展,可大大提升你的生产力
- windows shell命令大全_Windows shell
- Python中的lambda表达式
- python动图爱心表白_用python画动态图
- python笔记74- yaml 使用特殊符号| 解决字符串带换行的问题
- 【错误记录】Windows 控制台程序编译报错 ( WINDOWS.H already included. MFC apps must not #include <Windows.h> )
- 在Windows 10计算机上安装Python的最佳方法是什么?
- Python使用platform库获取系统信息:操作系统信息、硬件信息、python环境信息
- python在windows命令行下输出彩色文字代码详解编程语言
- 11个新手快速入门Python知识点!
- 和windows双系统安装:Linux与Windows并存(安装双系统linux)
- Python如何连接MySQL数据库(python怎么连接mysql数据库)
- 比较:Windows 与 Linux 的异同(windows和linux)
- python编程-将Python程序转化为可执行程序[整理]
- python生成器的使用方法
- python中list循环语句用法实例
- 用Python编写一个国际象棋AI程序
- Python中实现从目录中过滤出指定文件类型的文件