该程序是我学习Python的第一个小程序,用于熟悉Python语法,其中借鉴了很多其他网友的Code,望谅解。


参考列表

主要参考博客列表如下:

  1. LittleBearLi http://blog.csdn.net/u011584748/article/details/51377915
  2. http://www.itwendao.com/article/detail/362278.html

简介

本程序主要实现:抓取Bing的图片,并设置为桌面背景,设置该程序在开机时自动运行,便能在开机之后自动更改桌面背景。
主要有如下步骤:

  1. 从Bing抓取每日图片,保存到本地;
  2. 将抓取到的.JPG转换成.BMP格式;
  3. 将.BMP图片设置为桌面背景;
  4. 设置该程序在开机时自动运行。

前期准备工作

  1. 下载Python3(本例中使用python3.6.3),并设置环境变量
  2. 安装Pillow,该插件用于将.JPG转换为.BMP。建议使用pip进行安装,Python3中自带pip,只需在命令行中输入如下命令:
    pip install pillow
  3. 安装pywin32,该插件用于将.BMP图片设置为桌面背景。pywin32下载路径https://sourceforge.net/projects/pywin32/?source=navbar

Code

File1:GetBingPicture.py,用于从Bing抓取图片,并按照图片的日期保存到本地

#!/usr/bin/python3
#-*-coding:utf-8-*-
# Filename: GetBingPicture.pyimport os
from urllib import requestvar_BingPic_Dir = "F:\\flege\\Pictures\\BingPicture";
var_CurrentDayIndex = 0;#If the index is 0,it will get the pic js string of current day; If the index is 1,it will get the pic js string of last day.
var_Bing_Base_Url = "http://cn.bing.com";
var_BingPic_Url = "http://cn.bing.com/HPImageArchive.aspx?format=js&idx=" + str(var_CurrentDayIndex) + "&n=1&nc=1361089515117&FORM=HYLH1";#Function:  Get current day from js(year.month,day)
#In:        The full js string
#Out:       The string of current day
#Author:    Flege
#Date:      2017.08.25
def GetStr_CurrentDay(jsStr):print("GetStr_CurrentDay func:%s"%jsStr);jsStr = str(jsStr);idx = jsStr.index("startdate");print("The current day in js string index is: %d"%idx);currentDayStr = jsStr[(idx + len("startdate") + 3):((idx + len("startdate") + 3) + 8)];print(currentDayStr);#currentDayStr = "20180000";return currentDayStr;#Function:  Create a directory to storage bing pictures
#In:        The path of the directory
#Out:       The path of the directory
#Author:    Flege
#Date:      2017.08.25
def Create_Dir_Storage_Pic(path):path = path.strip();#except the spaceif 0 == len(path):return None;if not os.path.exists(path):os.makedirs(path,exist_ok=True);print("Create directory success,the pass is:%s"%(path));else:print("The directory is exist!");return path;#Function:  Get the path to storage the pic
#In:        The full js string
#Out:       The full path to storage the pic
#Author:    Flege
#Date:      2017.08.25
def Get_Path_to_Storage_Pic(jsStr):varPicPath = Create_Dir_Storage_Pic(var_BingPic_Dir);#Create file directoryvarPicPath += "\\" + GetStr_CurrentDay(jsStr);#Get the full pathreturn varPicPath;#Function:  Parser the pic url from js string
#In:        The full js string
#Out:       The url of pic
#Auhor:     Flege
#Date:      2017.08.25
def Parser_Pic_Url(jsStr):jsStr = str(jsStr);print("Parser_Pic_Url fun:%s"%jsStr);idx1 = jsStr.index("url");idx2 = jsStr.index("urlbase");print(idx1);print(idx2);outStr = jsStr[(idx1 + 6):(idx2 - 3)];print("The url of pic is:%s"%outStr);return outStr;#Function:  Get pic type
#In:        The full url of pic
#Out:       The string of pic type
#Auhor:     Flege
#Date:      2017.08.25
def Get_Pic_Type(picUrl):picUrl = str(picUrl);inLen = len(picUrl);print(inLen);typeIndex = picUrl.index(".",inLen - 5,inLen);print("Get_Pic_Type fun:%s"%picUrl);print(typeIndex);type = picUrl[typeIndex:];#type = ".jpg";return type;#Function:  Get the pictures by the given url and storage the pic to local
#In:        The url to get the bing pic js string
#Out:       None
#Author:    Flege
#Date:      2017.08.25
def Get_Pic():url = var_BingPic_Url;print("Get pictures from: %s"%url);webdata = request.urlopen(url);jsStr = webdata.read();#print("Get_Pic func:%s"%jsStr);picStoragePath = Get_Path_to_Storage_Pic(jsStr);picUrl = var_Bing_Base_Url + Parser_Pic_Url(jsStr);print(picUrl);picType = Get_Pic_Type(picUrl);print("The pic type is:%s"%picType);picFullPath = picStoragePath + picType;print("The pic full path is:%s"%picFullPath);try:request.urlretrieve(picUrl,(picStoragePath + picType));print("Storage file success!");#os.system("pause");#For Debugreturn picFullPath;except IOError:print("Storage file failed!!");os.system("pause");return None;return None;if __name__ == "__main__":Get_Pic();

File2:SetWindowsWallpaperByStealBing.py,将.JPG转换成.BMP格式,并设置为当前桌面

#!/usr/bin/python3
#-*-coding:utf-8-*-
# Filename: SetWindowsWallpaperByStealBing.pyimport os
import win32gui
import win32con
from PIL import Imageimport GetBingPicturevarStorageBMPPath = "F:\\flege\\Pictures\\BMP\\";#Function:  Set windows wallpaper by the given .BMP file path
#In:        The full path of .BMP file
#Out:       None
#Auhor:     Flege
#Date:      2017.08.26
def SetWindowsWallpaper(bmpFilePath):print("The pic path which will be set as wallpaper is:%s"%bmpFilePath);win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,bmpFilePath, win32con.SPIF_SENDWININICHANGE);#Only .BMP file can be set as the wallpaperreturn None;#Function:  Get the pic name from the full path of this pic
#In:        The full path of the pic;
#           The length of the pic type
#Out:       The pic name
#Auhor:     Flege
#Date:      2017.08.26
def Get_PicName(picPath,typeLen):picName = picPath[(len(picPath)- typeLen - 8):(len(picPath) - typeLen)];print("Pic name is:%s"%picName);return picName;#Function:  Conver the pic to .BMP file from the given pic path, and storage the .BMP file to the disk
#In:        The full path of pic
#Out:       The full path of .BMP file
#Auhor:     Flege
#Date:      2017.08.26
def ConvertPicTypeToBMP(picPath):print("Convert the pic type to .BMP");im = Image.open(picPath);print("Format:%s,Size:%s,Mode:%s"%(im.format,im.size,im.mode));picName = Get_PicName(picPath,len(str(im.format)));#Get the pic namebmpPath = varStorageBMPPath + picName + ".BMP";#Set the full path of .BMP file to storage itprint("BMP path:%s"%bmpPath);im.save(bmpPath);#Save the .BMP file to diskreturn bmpPath;if __name__ == "__main__":print("This is SetWindowsWallpaper.py");try:bmpFullPath = ConvertPicTypeToBMP(GetBingPicture.Get_Pic());#Conver the pic to .BMP,and return the .BMP pathSetWindowsWallpaper(bmpFullPath);#Set windows wallpaper by the given pic path#os.system("pause");#For Debugexcept IOError:print("Set windows wallpaper failed!");os.system("pause");

Last Step

最后的步骤便是将SetWindowsWallpaperByStealBing.py设置为开机自动运行。
打开注册表路径HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run新建字符串值,设置数值数据为SetWindowsWallpaperByStealBing.py的绝对路径即可。


成果展示

存储的.BMP文件截图

桌面背景


Python3抓取Bing每日图片做桌面背景,并设置为开机更新背景相关推荐

  1. Python3爬取Bing每日图片,并设置为电脑桌面

    文章目录 1 - 简述 2 - 核心代码 2.1 - 爬取BingImage 2.2 - 设置为桌面 2.3 - 设置为每日自动执行 3 - 完整代码 4 - 运行结果 作为鄙视链底层的"脚 ...

  2. 【PHP】使用PHP抓取Bing每日图像并为己所用

    Bing搜索的首页每天都会推送一张很漂亮的图片,把它保存下来,当做电脑桌面或是自己的网站背景图还不是美滋滋-- 今天的bing图片是这样的 既然要抓取这张图片,首先就得弄清这张图是从何而来的.经过对必 ...

  3. python自动换壁纸_Python爬取必应每日图片并实现Windows壁纸自动切换

    不知道大家是否对每日一成不变的壁纸感到厌倦呢?反正对于我个人来说,如果每天打开电脑映入眼帘的都是不同的画面,那么科研热情都会被充分激发,从而提高自己的劳动生产力. 原来使用的是Deepin系统,自己写 ...

  4. [Python] 抓取必应每日一图,设置为桌面壁纸

    在Windows 10 64位系统环境下,使用 Python 3.6 进行图片抓取和设置壁纸操作. 其中,图片链接 https://area.sinaapp.com/bingImg/ 为 Bing 每 ...

  5. 【Python爬虫学习实践】多线程爬取Bing每日壁纸

    在本节实践中,我们将借助Python多线程编程并采用生产者消费者模式来编写爬取Bing每日壁纸的爬虫.在正式编程前,我们还是一样地先来分析一下我们的需求及大体实现的过程. 总体设计预览 首先,我们先来 ...

  6. 获取Bing每日图片API接口

    bing图片每日更新,对于这一点感觉挺不错的,如果能够把bing每日图片作为博客背景是不是很不错呢?首先我们进入Bing首页,会发现自动转到中国版.不过这没关系,中国版更符合国情,速度也比国际版快一些 ...

  7. SEO优化:如何抓取手机网站图片

    企业在网站建设时,很多同时也做了手机端的网站,为了丰富网站的版面和内容,给网站添加了大量的图片做美化.网站的图片是一个网站能给用户最直观的信息表现,而对于搜索引擎而言,蜘蛛在随机抓取网站图片时候的识别 ...

  8. python抓取网站图片_python抓取图片示例 python抓取网页上图片

    python抓取网页上图片 这个错误时是什么意思 下面是代码 import re import urllib.request imp正则表达式匹配的url有错误 for x in add: print ...

  9. java 爬虫 抓取网上的图片报错521解决方案

    最近做爬虫时碰到了521错误,500开头的都是服务器错误:521错误码需要请求多次才能返回正确的结果:查看请求次数需要借助抓包工具,我自己使用Fiddler 4抓取到发送了三次请求才拿到结果,所以这就 ...

最新文章

  1. Forefront基本知识介绍
  2. 学python最好的方式-你们都是怎么学 Python 的?
  3. 马尔可夫“折棍子”过程 Markovian Stick-breaking Process 简介
  4. eclipse svn插件 不能正常显示属性的解决办法
  5. STM32F103_USART_GPIO配置及相应的IO口设置
  6. 二十三、Java类中重载和重写的区别
  7. vue render函数
  8. 工作184:自定义事件
  9. 解决黑苹果的887驱动问题
  10. python类的属性和对象属性_python 类属性、对象属性-阿里云开发者社区
  11. 王道 —— 操作系统的运行机制和体系结构
  12. 前台获取信息进行跳转
  13. 斗罗大陆服务器维护,04.28《斗罗大陆:武魂觉醒》停服维护公告(修罗1-7服先行服)...
  14. linux下软件的卸载,Linux下各种格式软件的安装及卸载方法
  15. 转:linux下挂载移动硬盘
  16. HttpServletRequest 和 HttpServletResponse
  17. 我的第一个WM5程序
  18. vb.net 画多个矩形_电气原理图和接线图识图方法,电气接线图怎么画?你会画吗?...
  19. 使用文本/CAD数据集添加地图注记
  20. P3537 [POI2012]SZA-Cloakroom

热门文章

  1. 重新定义公司:谷歌是如何运营的
  2. 【Axure高保真原型】航空信息可视化原型模板
  3. KVM qcow2、raw、vmdk等镜像格式和转换
  4. 江西省建筑节能与绿色建筑发展“十三五”规划
  5. 51单片机 串口通信
  6. Video标签自动启动迅雷,下载视频资源
  7. PDF批量加水印及加密解密
  8. Java基础教程(全代码解析)
  9. linux中goldendict发声词典播放报错问题解决方案
  10. PointPillars点云编码器代码运行过程中的问题及解决