python实现抓取必应图片设置桌面
源码参考https://github.com/vbirds/pyWallpaper,代码风格不错
本人只是将其适配到python3.5,并消除一些bug,源代码中桌面地址未使用绝对路径导致win10 下无法成功设置桌面
#python: 3.5
#os: win10 home
#-*-coding:utf-8-*-
from PIL import Image import win32gui import win32con import win32api import os import threading import urllib.request import time import sys import json''' {"images":[{"startdate":"20170827","fullstartdate":"201708271600","enddate":"20170828","url":"/az/hprichbg/rb/BotallackCornwall_ZH-CN11396172846_1920x1080.jpg","urlbase":"/az/hprichbg/rb/BotallackCornwall_ZH-CN11396172846","copyright":"康沃尔郡内的巴特莱克矿场, 英国 (© Robert Harding/Masterfile)","copyrightlink":"http://www.bing.com/search?q=botallack+manor&form=hpcapt&mkt=zh-cn","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20170827_BotallackCornwall%22&FORM=HPQUIZ","wp":true,"hsh":"5eb81f1029c57dfa1cc5f69ca871f4b1","drk":1,"top":1,"bot":1,"hs":[]}],"tooltips":{"loading":"正在加载...","previous":"上一个图像","next":"下一个图像","walle":"此图片不能下载用作壁纸。","walls":"下载今日美图。仅限用作桌面壁纸。"} } '''class Wallpaper:def __init__(self, time=60):self.count = 0if time <= 0 :self.time = 60self.time = timeself.urltemplate = 'http://cn.bing.com/HPImageArchive.aspx?format=js&idx=%d&n=1&nc=1361089515117&FORM=HYLH1'self.baImageUrlList = []self.localFileName = ''self.localBMPFileName = ''self.imagedir = './images/'self.bmpdir = './bmpimage/'self.bmplist = []def start(self):self.prepareDir()self.parserImageUrl()self.download_images()self.image_convert_bmp()self.set_wall_func()def prepareDir(self):if not os.path.exists(self.imagedir):os.makedirs(self.imagedir)if not os.path.exists(self.bmpdir):os.makedirs(self.bmpdir)def parserImageUrl(self):for i in range(0, 7, 1):url = self.urltemplate % i #use i replace format try:content = urllib.request.urlopen(url,None).read().decode("utf-8")# 由于有中文,decode("utf-8")必须except:print(url)print("parse try again")decodedjson = json.loads(content)imageurl = decodedjson['images'][0]['url']self.baImageUrlList.append('https://cn.bing.com'+imageurl)def download_images(self):for url in self.baImageUrlList:imagename = os.path.basename(url)imagepath = self.imagedir + imagenameprint(imagepath)f = open(imagepath, 'wb')try:conn = urllib.request.urlopen(url)except:print(url)print("download try again")f.write(conn.read())f.close()def image_convert_bmp(self):imaglist = os.listdir(self.imagedir)for imagepath in imaglist:file_name = os.path.basename(imagepath)file_name_type = os.path.splitext(file_name)file_name = file_name_type[0]newpath = self.bmpdir + file_name + '.bmp'imagepath = './images/' + imagepathbmpImage = Image.open(imagepath)bmpImage.save(newpath, "BMP")self.bmplist.append(sys.path[0]+'\\bmpimage\\'+file_name + '.bmp')def setWallpaper(self, 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")print(imagepath)win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2)#文件需要使用完整路径def set_wall_func(self):list_size = len(self.bmplist)index = self.count % list_sizefilename = self.bmplist[index]self.count += 1self.setWallpaper(filename)self.set_wall_timer()def set_wall_timer(self):timer = threading.Timer(self.time, self.set_wall_func)timer.start()if __name__ == '__main__':bing = Wallpaper(300)bing.start()
转载于:https://www.cnblogs.com/hixin/p/7444214.html
python实现抓取必应图片设置桌面相关推荐
- 利用selenium与etree抓取必应图片
利用selenium与etree抓取必应图片 文章目录 利用selenium与etree抓取必应图片 一.导入库 二.selenium模拟网页滚动 三.etree解析selenium保存的网页内容 四 ...
- python爬虫抓取百度图片_Python爬虫抓取百度的高清摄影图片
成果预览: 源代码: import requests import re url = 'https://image.baidu.com/search/index' headers = { 'User- ...
- Python多线程抓取网页图片地址
mini-spider 功能描述: 多线程网络爬虫,爬取网页图片地址(也可提取其他特征的URL) 使用python开发一个迷你定向抓取器mini_spider.py,实现对种子链接的广度优先抓取,并把 ...
- Python + PySpider 抓取百度图片搜索的图片
说明 1.PySpider 是一个方便并且功能强大的Python爬虫框架 2.PySpider 依赖于PhantomJS 3.windows平台,PySpider 与64位的Python兼容不太好,需 ...
- python:抓取必应高清壁纸第一张并设置为壁纸 修改注册表 重启壁纸不丢失
# -*- coding: utf-8 -*- import requests from lxml import etree #import re from re import split #bing ...
- Python爬虫抓取网页图片
本文通过python 来实现这样一个简单的爬虫功能,把我们想要的图片爬取到本地. 下面就看看如何使用python来实现这样一个功能. # -*- coding: utf-8 -*- import ur ...
- 用python每日更换“必应图片”为“桌面壁纸”
不少同学都知道,必应每天都会更新壁纸,都十分漂亮,有时候还十分惊艳,同时还会根据每个地区的特色应用不同的壁纸. 那么,如何让自己的电脑每天同步必应每日图片作为桌面背景呢? 下面用python脚本抓取必 ...
- python抓取网站图片_python抓取图片示例 python抓取网页上图片
python抓取网页上图片 这个错误时是什么意思 下面是代码 import re import urllib.request imp正则表达式匹配的url有错误 for x in add: print ...
- python爬虫学习基础教程,批量抓取美女图片!
python的抓取功能其实是非常强大的,当然不能浪费,呵呵.下面就与大家分享一个python写的美女图自动抓取程序吧! 其中用到urllib2模块和正则表达式模块.下面直接上代码: 用python批量 ...
最新文章
- Django-ondelete
- 苹果8如何设置锁屏无线网连接服务器,iPhone8屏幕解锁怎么设置?苹果iPhone8与8 Plus解锁四种方法...
- Git--团队开发必备神器
- LeetCode 21合并两个有序链表22括号生成
- 怎么安装python2.7_如何在Windows 7安装Python2.7
- SAP Spartacus split view右边视图的overflow属性三种不同的值
- c语言指着与数组,C语言指针与数组
- 前端学习(1269):axios的拦截器
- Leetcode--全排列(Java)
- 学习C/C++的简单方法
- 一首歌,一种情感,一种心情
- spring data jpa从入门到精通_程序员笔记:Spring Data JPA入门
- 9.20–9.24 三亚
- XML-RPC技术在WP上研究(一)
- MySQL误删数据救命指南:必收藏
- gradle 失败 编译项目_ElasticSearch编译时如何选择Gradle版本?
- python云台控制原理_python伺服云台摄像头图像作为背景
- mybatis使用truncate清空表
- 合理运营在线教育社群
- 发明现代概率的男人:他如何发现“不可能”对人类事务的影响?