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

yipeiwu_com5年前Python基础

微软最近出了个 必应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()

相关文章

python多个模块py文件的数据共享实例

模块a.py 想用 b.py中公有数据 cnt b的python文件 #!/usr/bin/env python # coding:utf8 from wx import Cal...

详解Python数据分析--Pandas知识点

详解Python数据分析--Pandas知识点

本文主要是总结学习pandas过程中用到的函数和方法, 在此记录, 防止遗忘 1. 重复值的处理 利用drop_duplicates()函数删除数据表中重复多余的记录, 比如删除重复多余...

通过 Python 和 OpenCV 实现目标数量监控

通过 Python 和 OpenCV 实现目标数量监控

今天我们将利用python+OpenCV实现对视频中物体数量的监控,达到视频监控的效果,比如洗煤厂的监控水龙头的水柱颜色,当水柱为黑色的超过了一半,那么将说明过滤网发生了故障。当然不仅如...

flask 使用 flask_apscheduler 做定时循环任务的实现

我是初学者,对 flask 很陌生,网上搜到的文章都看不懂,很尴尬。 本意是打算对广发信用卡diy卡积分兑换签帐额的数量进行爬虫监控。将抓取到的余量通过钉钉机器人发送到群里。爬虫代码就...

Python 中的 import 机制之实现远程导入模块

Python 中的 import 机制之实现远程导入模块

所谓的模块导入( import ),是指在一个模块中使用另一个模块的代码的操作,它有利于代码的复用。 在 Python 中使用 import 关键字来实现这个操作,但不是唯一的方法,还有...