Python抓取每日必应壁纸
#
作者:Zhanling
#
import urllib2
import re
import os
import time
import thread
import logging
class BingBG:
def __init__(self): #初始化成员变量
mTime = time.time()
mOldTime = time.localtime(mTime)
self.strOldTime = str(mOldTime.tm_year) + str(mOldTime.tm_mon) + str(mOldTime.tm_mday)
self.strNewTime = str("") #OldTime与NewTime结合判断日期是否变更
self. myUrl = "http://cn.bing.com"
self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' #伪装浏览器访问
self.headers = {'User-Agent': self.user_agent}
self.picDir = r"D:\BingBG" #图片保存地址
logging.info("init app") #初始化完成
def DownLoad(self): #下载
req = urllib2.Request(self.myUrl, headers=self.headers) #封装请求
logging.info("opening cn.bing.com")
try:
res = urllib2.urlopen(req) #打开必应
page = res.read() #读取网页内容
items = re.findall(r"g_img={url: \".*\.jpg\"", page) #正则匹配图片链接
if items:
mTime = time.time() #获取当前日期
mLocalTime = time.localtime(mTime)
strLocalTime = str(mLocalTime.tm_year) + str(mLocalTime.tm_mon) + str(mLocalTime.tm_mday)
picUrl = self.myUrl + items[0][13:-1] #拼接完整的必应图片地址
img_name = self.picDir + "\\" + strLocalTime + ".jpg" #以当前日期作为文件名
img_data = urllib2.urlopen(picUrl).read() #读取图片内容
f = open(img_name, 'wb') #保存到文件
f.write(img_data)
f.close()
logging.info("download success")
else:
logging.info("item not found")
except: #出现异常
logging.info("URL open exception")
def DownLoadThread(self): #循环线程
logging.info("thread start")
while(1):
if not os.path.exists(self.picDir): #判断是否存在保存图片的文件夹,不存在则创建
os.mkdir(self.picDir)
logging.info("not exist dir,create")
if not os.listdir(self.picDir): #判断文件夹下面有没有文件,没有则马上下载当天壁纸
mTime = time.time()
mOldTime = time.localtime(mTime)
self.strOldTime = str(mOldTime.tm_year) + str(mOldTime.tm_mon) + str(mOldTime.tm_mday)
logging.info("empty, first download")
self.DownLoad()
else: #存在文件,判断日期是否变更
mTime = time.time()
mNewTime = time.localtime(mTime)
self.strNewTime = str(mNewTime.tm_year) + str(mNewTime.tm_mon) + str(mNewTime.tm_mday)
if self.strNewTime != self.strOldTime: #日期变更则下载图片
self.strOldTime = self.strNewTime
logging.info("check, day change, download")
self.DownLoad()
else:
logging.info("check, day not change, keep on")
time.sleep(14400) #四小时循环一次
def start(self):
thread.start_new_thread(self.DownLoadThread())
logging.basicConfig(level=logging.DEBUG, #Log配置
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='BingBG.log',
filemode='a')
bingBg = BingBG() #实例化对象
bingBg.start() #线程启动