每天换一个壁纸,每天好心情。
# -*- coding: UTF-8 -*- from __future__ import unicode_literals
import Image
import datetime
import win32gui,win32con,win32api
import re
from HttpWrapper import SendRequestStoreFolder = "c:\\dayImage"def setWallpaperFromBMP(imagepath):k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\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)def setWallPaper(imagePath):"""
    Given a path to an image, convert it to bmp and set it as wallpaper"""
    bmpImage = Image.open(imagePath)newPath = StoreFolder + '\\mywallpaper.bmp'bmpImage.save(newPath, "BMP")setWallpaperFromBMP(newPath)def getPicture():url = "http://photography.nationalgeographic.com/photography/photo-of-the-day/"h = SendRequest(url)if h.GetSource():r = re.findall('<div class="download_link"><a href="(.*?)">Download',h.GetSource())if r:return SendRequest(r[0]).GetSource()else:print "解析图片地址出错,请检查正则表达式是否正确"return Nonedef setWallpaperOfToday():img = getPicture()if img:path = StoreFolder + "\\%s.jpg" % datetime.date.today()f = open(path,"wb")f.write(img)f.close()setWallPaper(path)setWallpaperOfToday()
print 'Wallpaper set ok!'

其中的httpwrapper是我写的一个http访问的封装:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#-------------------------------------------------------------------------------
# Name: 对http访问的封装
#
# Author: qianlifeng
#
# Created: 10-02-2012
#-------------------------------------------------------------------------------import base64
import urllib
import urllib2
import time
import re
import sysclass SendRequest:"""
  网页请求增强类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,默认为Getauth: 'base' 或者 'cookie'user: 用于base认证的用户名password: 用于base认证的密码cookie: 请求附带的cookie,一般用于登录后的认证其他头信息:e.g. referer='www.sina.com.cn'"""
self.url = urlself.data = dataself.method = methodself.auth = authself.user = userself.password = passwordself.cookie = cookieif 'referer' in header:self.referer = header[referer]else:self.referer = Noneif 'user-agent' in header:self.user_agent = header[user-agent]else:
## self.user_agent = 'Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0'self.user_agent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16'self.__SetupRequest()self.__SendRequest()def __SetupRequest(self):if self.url is None or self.url == '':raise 'url 不能为空!'#访问方式设置if self.method.lower() == 'post':self.Req = urllib2.Request(self.url, urllib.urlencode(self.data))elif self.method.lower() == 'get':if self.data == None:self.Req = urllib2.Request(self.url)else:self.Req = urllib2.Request(self.url + '?' + urllib.urlencode(self.data))#设置认证信息if self.auth == 'base':if self.user == None or self.password == None:raise 'The user or password was not given!'else:auth_info = base64.encodestring(self.user + ':' + self.password).replace('\n','')auth_info = 'Basic ' + auth_infoself.Req.add_header("Authorization", auth_info)elif self.auth == 'cookie':if self.cookie == None:raise 'The cookie was not given!'else:self.Req.add_header("Cookie", self.cookie)if self.referer:self.Req.add_header('referer', self.referer)if self.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().dictself.Res.close()except:print "Error: HttpWrapper=>_SendRequest ", sys.exc_info()[1]def GetResponseCode(self):"""
    得到服务器返回的状态码(200表示成功,404网页不存在)"""
    return self.codedef GetSource(self):"""
    得到网页源代码,需要解码后在使用"""
    if "source" in dir(self):return self.sourcereturn u''def GetHeaderInfo(self):"""
    u'得到响应头信息'"""
    return self.head_dictdef GetCookie(self):"""
    得到服务器返回的Cookie,一般用于登录后续操作"""
    if 'set-cookie' in self.head_dict:return self.head_dict['set-cookie']else:return Nonedef GetContentType(self):"""
    得到返回类型"""
    if 'content-type' in self.head_dict:return self.head_dict['content-type']else:return Nonedef GetCharset(self):"""
    尝试得到网页的编码如果得不到返回None"""
    contentType = self.GetContentType()if contentType is not None:index = contentType.find("charset")if index > 0:return contentType[index+8:]return Nonedef GetExpiresTime(self):"""
    得到网页过期时间"""
    if 'expires' in self.head_dict:return self.head_dict['expires']else:return Nonedef GetServerName(self):"""
    得到服务器名字"""
    if 'server' in self.head_dict:return self.head_dict['server']else:return None__all__ = [SendRequest,]if __name__ == '__main__':b = SendRequest("http://www.baidu.com")print b.GetSource()

本文转载自:http://www.cnblogs.com/qianlifeng/archive/2012/05/10/2494005.html

转载于:https://www.cnblogs.com/icamel/archive/2012/05/15/2502117.html

python设置windows桌面壁纸相关推荐

  1. python代码壁纸-python设置windows桌面壁纸的实现代码

    代码如下: # -*- coding: UTF-8 -*- from __future__ import unicode_literals import Image import datetime i ...

  2. python代码桌面壁纸_Python实现设置windows桌面壁纸代码

    Python实现设置windows桌面壁纸代码 发布于 2015-04-07 16:59:42 | 122 次阅读 | 评论: 0 | 来源: 网友投递 Python编程语言Python 是一种面向对 ...

  3. Python——实现Windows桌面壁纸和bing背景的同步

    目的 实现Windows桌面壁纸和bing背景的同步 如何实现 requests(获取url) json(解析对象) os.path(设置图片保存路径以及日志信息) ctypes(设置Windows壁 ...

  4. 设置Windows桌面壁纸

    最近发现一个API可以设置Windows系统的桌面壁纸,感觉挺好玩 代码: #include <stdio.h> #include <Windows.h>int main() ...

  5. python代码桌面壁纸_Python实现设置windows桌面壁纸代码分享

    每天换一个壁纸,每天好心情. # -*- coding: UTF-8 -*- from __future__ import unicode_literals import Image import d ...

  6. python壁纸程序代码_python设置windows桌面壁纸的实现代码

    #!/usr/bin/env python # -*- coding: UTF-8 -*- #----------------------------------------------------- ...

  7. python代码手机壁纸_Python实现设置windows桌面壁纸代码分享

    #!/usr/bin/env python # -*- coding: UTF-8 -*- #----------------------------------------------------- ...

  8. python修改电脑桌面壁纸_python设置windows桌面壁纸的方法

    #!/usr/bin/env python # -*- coding: UTF-8 -*- #----------------------------------------------------- ...

  9. python修改桌面壁纸_python设置windows桌面壁纸

    #!/usr/bin/env python # -*- coding: UTF-8 -*- #----------------------------------------------------- ...

最新文章

  1. joc杂志影响因子2019_2019年放射学领域SCI主要杂志影响因子汇总
  2. Java中使用Jedis连接Redis对List进行操作的常用命令
  3. BZOJ1555 KD之死
  4. 【机器学习基础】深入讨论机器学习 8 大回归模型的基本原理以及差异!
  5. wxWidgets:wxContextMenuEvent类用法
  6. 怎么解决input中readonly属性的iOS一直存在光标问题
  7. DataNucleus 3.0与Hibernate 3.5
  8. GCC 常见参数配置
  9. Digits of Factorial LightOJ - 1045(数学题?)
  10. 小前端眼里的大前端:GMTC 2018 参会小结
  11. 祝贺VMware中文网站正式发布!
  12. 华为击败思科 赢得阿曼2600万美元NGN合同
  13. java考试时间,Java认证考试知识点:Java时间类的函数
  14. 服务器硬盘容量为0,硬盘容量不一样 raid0 扩容也可以很自如?
  15. 纸张的规格:A3.A4.A5.A6纸的尺寸大小
  16. PBI培训(3):Power BI主题设置方法汇总及示例
  17. Beego-HelloWorld
  18. 百度无人驾驶网约车起步价16元;美团回应共享单车涨价;谷歌公开抨击苹果阻碍跨平台交流|极客头条
  19. 给Docker中的Nginx搭建HTTPS环境
  20. 《BIGEMAP地图软件》荣登2017、2018年度论坛《最受欢迎谷歌地图软件》

热门文章

  1. 基于open cv 的人脸识别程序
  2. 每周读书#6 - 《写给大家的中国美术史》
  3. vue3.0清理定时器无效问题
  4. 出行即服务MAAS专辑(2022)(可下载)
  5. Codeforces Round #143 (Div. 2) (ABCD 思维场)
  6. SCI投稿7个阶段的邮件模板!
  7. 2020学期学习计划
  8. 市值破7000亿美元 贝索斯成全球新首富,成就亚马逊的正是人工智能
  9. 天才小毒妃 第973章 韩芸汐,救我
  10. 一文了解波卡上的跨链协议X Protocol :基于web3.0将应用链接元宇宙的有效途径