Python--网页更新监控工具
Python--网页更新监控工具
(2012-08-04 17:29:05)
标签:
网页监控更新pythonit |
分类:程序人生 |
一、工作原理
首先是下载网页,这个在网上有太多的案例了,就不详细谈了。但是要注意对网页的中文字符的处理,这是python一直存在的问题。这里使用了chardet这个第三方包,能够对网页的编码进行测试,给出网页的实际编码。网页下载后,保存时候要按系统默认的编码进行保存,否则或出现乱码,最好是保存为二进制文件。
然后对html文件进行过滤,提取出网页的文本内容。这里网上也有很多案例。经过比对,我只是使用了正则
表达式来进行过滤,发现效果和效率都不错。
最后,就是对不同时间段抓取的网页内容进行比对。这里使用了difflib模块,只要两者有差异,就给出详细
差异结果。本工具的测试环境是:win7+python2.7.3,读者可以自行写个win系统的批处理脚本,定时调用此工具。第一次运行时候是没有结果输出的,因为第一次只是执行下载网页,还有比对的样本。还有就是,如果发现网页有更新,需要及时更新上一次的比对文件(txt格式文本)。
二、代码文件结构
1)downloadHtml 下载文件的模块
2)getEncoding 获得文件编码模块
3)Html2Text 提取文本内容模块
4)differFile 比对文件差异模块
5)monitorHtml 执行监控网页更新模块
三、测试截图
1)网页没有更新
留意有下划线部分内容,这就是网页内容有差异的部分。
四、主要代码
-----------downloadHtml-----------------------------
import urllib
import urllib2
import getEncoding
import sys
def downloadHtml(websize, savefile):
'''
this methodis used download html,but if html contain chinese charateres
should notuse this method
'''
#At firstcheck the encoding of html
encoding =getEncoding.quick_getHtmlEncoding(websize)
content =urllib2.urlopen(websize).read()
type =sys.getfilesystemencoding()
s =content.decode(encoding).encode(type)
file =open(savefile, 'wb')
file.write(s)
file.close()
-----------differFile----------------------
import difflib
def isDiff(srcfile, tarfile):
'''
compare withtwo files,if equal then return ture
'''
src =file(srcfile).read().split(' ')
tar =file(tarfile).read().split(' ')
ret =1
# ignoreblank lines
temp =difflib.SequenceMatcher(lambda x: len(x.strip()) == 0, src,tar)
for tag, i1,i2, j1, j2 in temp.get_opcodes():
#print tag
if tag != 'equal':
ret = 0
break
return (Trueif ret == 1 else False)
def getDetails(srcfile, tarfile, flag = 'all'):
'''
compare wtihtwo files,if different then output details
'''
temp1_context = file(srcfile).read()
temp2_context = file(tarfile).read()
file1_context = temp1_context.splitlines()
file2_context = temp2_context.splitlines()
diff =difflib.Differ().compare(file1_context, file2_context)
if flag =='all':
#output all context
print "\n".join(list(diff))
else:
#only output different part of context
linenum = 1
for line in diff:
if line[0] != ' ':
print 'line:%d %s'%(linenum, line)
else:
linenum = linenum + 1
--------monitorHtml-------------------------
import downloadHtml
import differFile
import Html2Text
import os.path
def isExists(saveFile):
'''
check filewhether existed
'''
returnos.path.isfile(saveFile)
def monitorHtml(websize, savehtml, savetxt, originaltxt):
'''
monitorassign html, if context of html has changed then outputdetails
'''
downloadHtml.downloadHtml(websize, savehtml)
ifisExists(originaltxt):
Html2Text.Html2Txt(savehtml, savetxt)
if(differFile.isDiff(originaltxt, savetxt)):
print 'These two files are equal.'
else:
print 'These two files are different:'
differFile.getDetails(originaltxt, savetxt,'notall')
else:
Html2Text.Html2Txt(savehtml, originaltxt)
if __name__ == '__main__':
websize1 ='http://www.baidu.com'
srcname1 ='E:\pyproj\differHtml\orginal.txt'
htmlname1 ='E:\pyproj\differHtml\src.htm'
txtname1 ='E:\pyproj\differHtml\src2txt.txt'
monitorHtml(websize1, htmlname1, txtname1, srcname1)
------------------------------------
ps: 完整代码下载
Python--网页更新监控工具相关推荐
- python监控网页更新_【小白教程】Python3监控网页
之前用RSS来监控网页更新内容,可惜刷新时间太长了,三个小时..只能看看新闻啥的,又没有小钱钱充会员(摊手 听说Python可以做这个功能,抱着试试看的态度,本以为会很麻烦,没想到这么简单哈哈~我从来 ...
- python 网页编程_通过Python编程检索网页
python 网页编程 The internet and the World Wide Web (WWW), is probably the most prominent source of info ...
- python自动更新excel_Python办公自动化(六)|自动更新表格,告别繁琐
今天我们讲解的案例是如何使用Python自动更新Excel表格,简单来说就是每天都会对Excel中多个sheet进行更新,需要操作完后可以用程序完成第一张sheet 汇总表的更新,大概就是这样 当然实 ...
- python网页爬虫-python网页爬虫浅析
Python网页爬虫简介: 有时候我们需要把一个网页的图片copy 下来.通常手工的方式是鼠标右键 save picture as ... python 网页爬虫可以一次性把所有图片copy 下来. ...
- python网页爬虫-Python网页爬虫
曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然开发语言是C/C++,但平时的很多文本数据处理任务都交给了Python.离开腾讯创业后,第一个作品课程图谱也是选 ...
- python如何更新包_python如何更新包 python更新包代码示例
python如何更新包?本篇文章小编给大家分享一下python更新包代码示例,代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看. Python安装新包,pip是 ...
- visual studio无法更新_微软发布 VS Code Python 四月更新
微软发布了 4 月的 Visual Studio Code Python 扩展更新,该版本包括 Jypyter Notebooks 中的 ipywidgets 支持以及 Django 和 Flask ...
- visual studio code Python终端运行_VS Code 6 月 Python 扩展更新
优质文章,第一时间送达! 微软发布了 6 月的 Visual Studio Code Python 扩展更新,此版本除了解决 53 个问题外,还开发了新功能,例如逐行运行和启动页面. 逐行运行(Run ...
- Visual Studio Code 202008 Python 扩展更新
微软发布了 8 月的 Visual Studio Code Python 扩展更新,此版本总共修复了总共 38 个问题. 主要更新内容包括: 支持多个 Python 交互式窗口 这是用户呼声最高的功能 ...
- Visual Studio Code 10 月 Python 扩展更新
微软发布了 10 月的 Visual Studio Code Python 扩展更新,该版本包括支持原生编辑 Juypter Notebook,添加在终端中运行 Python 文件的按钮,以及改进 P ...
最新文章
- 软件定义闪存存储系统关键技术
- 吴恩达 coursera ML 第十六课总结+作业答案
- 远程唤醒、WOL、Magic_Packet【转】
- 1156: 单数变复数
- linux的htb队列,Linux流量控制中的HTB队列创建与过滤(2)
- 2010中国移动开发者大会21日开幕 揭密五大亮
- vue2实现传送门效果
- vue打印插件,使用教程
- Coding and Paper Letter(七十五)
- python一张纸折叠到珠峰高度_python实现seo疯狂外链发送工具
- execution plan mysql_MySQL Execution Plan--合理利用隐式的业务逻辑
- linux查看邮件服务状态,利用mail实时监测服务器程序状态
- 云原生k8s的前世今生--Docker
- HDC.Cloud 华为开发者大会2021.04.24 学习记录
- php微信获取模板id,微信模板消息示例
- 记录一下网络爬虫实战scrapy---某蜜蜂网站
- OSChina 周二乱弹 ——震惊!女友当着男友在大众面前竟和陌生男人做出这事!...
- sell 15 digital photo frame with CATV and bluetooth
- 简单、好用才是真道理,适用于初学人群的Linkboy5.0
- 塔菲尔曲线如何分析_浅谈塔菲尔动力学(Tafel Kinetics)