首页 编程教程正文

使用Python脚本将Bing的每日图片作为桌面的教程

piaodoo 编程教程 2020-02-02 12:20:57 943 0 python教程

这篇文章主要介绍了使用Python脚本将Bing的每日图片作为桌面的教程,示例基于Windows操作系统环境实现,需要的朋友可以参考下

微软最近出了个 必应bing 缤纷桌面,使用下来还是不错,可以每天更换Bing首页的北京作为壁纸,但是该软件有个不好的地方是,安装后桌面上会有一个搜索框出现,很是烦人,而且不能关掉。于是出于技术考虑,想到了使用Python来实现这个功能。

正如很多介绍Python书中那样,Python是中胶水语言,用在哪里都是可行的。想要使用Python给桌面设置背景只需要下个模块安装即可:

http://sourceforge.net/projects/pywin32/

代码非常简单,参考了网上一些其他人写了代码,具体代码如下:
 

# -*- coding: utf-8 -*-
 
import urllib,time,os,Image,win32gui,win32con,win32api
 
class StealBing:
 
 def __init__(self):
  self.content = urllib.urlopen('http://cn.bing.com/').read()
  self.bgImageUrl = ''
  self.localFileName = ''
  self.localBMPFileName = ''
 
 def parserImageURL(self):
  tempStr = self.content[self.content.index('g_img={url:')+12:]
  self.bgImageUrl = tempStr[:tempStr.index('id:\'bgDiv\'')-2]
 
 def createLocalFileName(self):
  randomStr = time.strftime("%Y%m%d", time.localtime())
  self.localFileName = 'D:/Bing/' + randomStr + '.jpg'
  self.localBMPFileName = 'D:/Bing/' + randomStr + '.bmp'
 
 def downloadImage(self):
  if self.bgImageUrl == '':
   self.parserImageURL()
  if self.localFileName == '':
   self.createLocalFileName()
  urllib.urlretrieve(self.bgImageUrl, self.localFileName)
 
 def updateBGImage(self):
  img = Image.open(self.localFileName)
  img.save(self.localBMPFileName)
  os.remove(self.localFileName)
  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, self.localBMPFileName , 1+2)
 
if __name__ == '__main__':
 stealBing = StealBing()
 stealBing.downloadImage()
 stealBing.updateBGImage()

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:https://www.piaodoo.com/1001.html

评论

搜索