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 = 0
if time <= 0 :
self.time = 60
self.time = time
self.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 + imagename
print(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/' + imagepath bmpImage = 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拉伸适应桌面,0桌面居中
win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "")
print(imagepath)
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2)#文件需要使用完整路径 def set_wall_func(self):
list_size = len(self.bmplist)
index = self.count % list_size
filename = self.bmplist[index]
self.count += 1
self.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()
python实现抓取必应图片设置桌面的更多相关文章
- Python实用案例,Python脚本,Python实现每日更换“必应图片”为“桌面壁纸”
往期回顾 Python实现自动监测Github项目并打开网页 Python实现文件自动归类 Python实现帮你选择双色球号码 前言: 今天我们就利用python脚本实现每日更换"必应图片& ...
- python+requests抓取页面图片
前言: 学完requests库后,想到可以利用python+requests爬取页面图片,想到实战一下.依照现在所学只能爬取图片在html页面的而不能爬取由JavaScript生成的图片,所以我选取饿 ...
- Python爬虫 —— 抓取美女图片
代码如下: #coding:utf-8 # import datetime import requests import os import sys from lxml import etree im ...
- Python爬虫 —— 抓取美女图片(Scrapy篇)
杂谈: 之前用requests模块爬取了美女图片,今天用scrapy框架实现了一遍. (图片尺度确实大了点,但老衲早已无恋红尘,权当观赏哈哈哈) Item: # -*- coding: utf-8 - ...
- python爬虫-抓取acg12动漫壁纸排行设置为桌面壁纸
ACG-wallpaper 初学python,之前想抓取P站的一些图片来着,然后发现acg12这里有专门的壁纸榜单,就写了个抓取壁纸作为mac桌面壁纸玩玩. 功能:抓取acg12壁纸榜单的动漫壁纸,并 ...
- Python 爬虫: 抓取花瓣网图片
接触Python也好长时间了,一直没什么机会使用,没有机会那就自己创造机会!呐,就先从爬虫开始吧,抓点美女图片下来. 废话不多说了,讲讲我是怎么做的. 1. 分析网站 想要下载图片,只要知道图片的地址 ...
- python 爬虫抓取心得
quanwei9958 转自 python 爬虫抓取心得分享 urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quo ...
- Python数据抓取技术与实战 pdf
Python数据抓取技术与实战 目录 D11章Python基础1.1Python安装1.2安装pip1.3如何查看帮助1.4D1一个实例1.5文件操作1.6循环1.7异常1.8元组1.9列表1.10字 ...
- php远程抓取网站图片并保存
以前看到网上别人说写程序抓取网页图片的,感觉挺神奇,心想什么时候我自己也写一个抓取图片的方法! 刚好这两天没什么事,就参考了网上一个php抓取图片代码,重点借鉴了 匹配img标签和其src属性正则的写 ...
随机推荐
- 通过设置标签class值控制标签的显示与隐藏
需求背景如下: 原项目居民.单位.计量三模块共用一个jsp文件,显示的页面也顺理成章的统一了,幸亏没有调用同一个js,在此基础上要求居民和单位计量的分离,即居民的显示居民的相关信息,单位和计量的显示相 ...
- Some Principles
立刻做 1.2分钟原则 凡是2分钟内就可以完成的事,立刻去做不要犹豫.人的大脑擅长分析处理,不擅长记忆. 应用举例: a.加微信加QQ顺手添加备注名,或许下次联系已经是三个月后了. b.吃完饭立刻洗碗 ...
- Android 自己定义ViewGroup手把手教你实现ArcMenu
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37567907 逛eoe发现这种UI效果,感觉非常不错,后来知道github上有这 ...
- FreeSWITCH技巧:如何向通话的另一方号码发送dtmf?
注:这里的文章都是本人的日常总结,请尊重下个人的劳动成果,转载的童鞋请注明出处,谢谢. 如您转载的文章发生格式错乱等问题而影响阅读,可与本人联系,无偿提供本文的markdown源代码. 联系邮箱:ji ...
- atitit. it软件项目管理---自己的员工,雇佣军、援军,混合的员工 杂牌 人员管理架构
atitit. it软件项目管理---自己的员工,雇佣军.援军,混合的员工 杂牌 人员管理架构 1. 企业的正规军,雇佣军,杂牌划分 1 1.1. 企业的员工基本是雇佣而来 1 1.2. 全职员工与兼 ...
- PCIe学习笔记(15)--- TLP的ROUTING方式
PCIE是POINT TO POINT的,不像PCI,是SHARED-BUS,总线上的数据,是被所有EP DEV看到的. 这一点与USB2.0比较类似,是广播方式的(BROADCASTING) USB ...
- twemproxy源码分析2——守护进程的创建
twemproxy源码中关于守护进程的创建实现得比较标准,先贴出代码来,然后结合一些资料来分析和列举一些实现守护进程的常用方法,不过不得不说twemproxy的实现确实是不错的,注释都写在了代码中,直 ...
- iOS开发之使用XMPPFramework实现即时通信
iOS开发之使用XMPPFramework实现即时通信 关于XMPP的理论介绍在本篇博客中就不做赘述了,如何在我们之前的微信中加入XMPP协议来实现通信呢?下面将会介绍一下XMPP的基本的知识,让 ...
- 浅谈P2P终结者原理及其突破
P2P终结者按正常来说是个很好的网管软件,但是好多人却拿它来,恶意的限制他人的流量,使他人不能正常上网,下面我们就他的功能以及原理还有突破方法做个详细的介绍! 我们先来看看来自在网上PSP的资料:P2 ...
- centos 系统管理维护指南
# centos 系统管理维护指南 centos系统是服务器的首选系统,系统运维支持需要的内容汇总整理如下. ### 系统管理------------------------------ 查看系统版本 ...