很多网页都会自动更新一些内容上去
比如说:http://www.opensolaris.org/os/community ... -days/all/
我想知道怎么用python写个脚本自动抓取那个网页上的更新内容
自己有一点点思路,就是把把网页的内容抓下来,然后和原来的内容比较(一开始保存一个网页内容作为比较对象,需要时就更新这个文件),把更新的内容提取出来 最后mail出去
但是我现在只会一点python的简单语法,怎么抓取网页和分析html都不大会
请大虾指点,谢谢~
[python]如何抓取网页的更新
- star
- 帖子: 76
- 注册时间: 2007-01-16 21:27
- 来自: 上海
- 联系:
[python]如何抓取网页的更新
_______________________________________
Ubuntu is not my all
But it makes me enjoy all my life
Ubuntu is not my all
But it makes me enjoy all my life
- star
- 帖子: 76
- 注册时间: 2007-01-16 21:27
- 来自: 上海
- 联系:
Re: [python]如何抓取网页的更新
search了一下午
误打误撞弄了一个基本能用的:
下一步是通过结果把更新发出去~
误打误撞弄了一个基本能用的:
代码: 全选
#coding=utf-8
#需要BeautifulSoup 支持:http://crummy.com/software/BeautifulSoup
import re
import urllib
import urllib2
import xml.dom.minidom
from xml.sax.saxutils import unescape
from BeautifulSoup import BeautifulSoup # For processing HTML
def formalize(text):
result = ''
lines = text.split(u'\n')
for line in lines:
line = line.strip()
if len(line) == 0:
continue
result += line + u'\n'
return result
def formalize_better(text):
result = ''
version = ''
tmp = ''
lines = text.split(u'\n')
lens = len(lines)
i = 0
while i < lens:
line = lines[i].strip()
if len(line) == 0:
i = i + 1
elif re.match('Build [0-9]+',line) is not None:
version = line
i = i + 1
else:
tmp = version + u' ' + line + u' ' + lines[i+1].strip() + u'\n'
result += tmp
i = i + 2
return result
def remove_tag(origin):
minisoup = BeautifulSoup(origin)
text = ''.join([e for e in minisoup.recursiveChildGenerator() if isinstance(e, unicode)])
text = urllib.unquote(unescape(text, {'"':'"'}))
print "begin to fomalize~"
text = formalize_better(formalize(text)).encode("utf-8")
return text
updatefile = open("opensolaris_update.txt", "w")
count = 0
proxy_support = urllib2.ProxyHandler({"http" : "http://proxy01.pd.intel.com:911"})
opener = urllib2.build_opener(proxy_support)
# install it
urllib2.install_opener(opener)
url = "http://www.opensolaris.org/os/community/on/flag-days/all/"
data = urllib2.urlopen(url).readlines()
soup = BeautifulSoup("".join(data))
updates = str(soup.findAll('tbody')[0])
text = remove_tag(updates)
print >> updatefile, text
updatefile.close()
updatefile = open("opensolaris_update.txt", "r")
backupfile = open("opensolaris_backup.txt", "r")
resultfile = open("opensolaris_update_result", "w")
backup_contents = backupfile.readline()
for update_contents in updatefile.readlines():
if update_contents == backup_contents or update_contents == '':
break
else:
print >> resultfile, update_contents
updatefile.close()
backupfile.close()
resultfile.close()
_______________________________________
Ubuntu is not my all
But it makes me enjoy all my life
Ubuntu is not my all
But it makes me enjoy all my life
- anticlockwise
- 帖子: 2394
- 注册时间: 2007-03-01 20:46
- 来自: 湖南长沙
Re: [python]如何抓取网页的更新
不错,不过检测网页更新,建议楼主可以看看Information Retrieval相关的文章
网页更新检测有许多算法,其中一个我知道的是用哈希来做的~~
原理简单说来就是:将网页与某个Hash建立映射,在网页更新的时候,再次生成这个Hash的时候会与原来的不一样。这个算法的重点在于哈希函数的定义,如何能够定义出使网页的哈希唯一而又高效的一个哈希函数是重要的,不过很多人已经提出了各种哈希函数~~
在知道网页更新了以后,提取内容就是另一个问题了,呵呵~~最简单的就是用diff啦~~
网页更新检测有许多算法,其中一个我知道的是用哈希来做的~~
原理简单说来就是:将网页与某个Hash建立映射,在网页更新的时候,再次生成这个Hash的时候会与原来的不一样。这个算法的重点在于哈希函数的定义,如何能够定义出使网页的哈希唯一而又高效的一个哈希函数是重要的,不过很多人已经提出了各种哈希函数~~
在知道网页更新了以后,提取内容就是另一个问题了,呵呵~~最简单的就是用diff啦~~
-
- 帖子: 1
- 注册时间: 2009-12-08 22:23
Re: [python]如何抓取网页的更新
请问3楼 Information Retrieval是什么?是一个可供检索的库么?谢谢。
- anticlockwise
- 帖子: 2394
- 注册时间: 2007-03-01 20:46
- 来自: 湖南长沙
Re: [python]如何抓取网页的更新
额……这位是马甲吧……0000069958 写了:请问3楼 Information Retrieval是什么?是一个可供检索的库么?谢谢。
不管了,Information Retrieval简单点说是自然语言处理的一个技术,例如搜索引擎就是一种Information Retrieval
- eexpress
- 帖子: 58428
- 注册时间: 2005-08-14 21:55
- 来自: 长沙
-
- 帖子: 3
- 注册时间: 2009-12-10 0:36
Re: [python]如何抓取网页的更新
我也做了一个
但是不知道为什么
老是获取不到新的内容
在浏览器上已经可以看到更新的内容了
但是urlopen。read()出来的还是以前的内容!!
郁闷啊!!
但是不知道为什么
老是获取不到新的内容
在浏览器上已经可以看到更新的内容了
但是urlopen。read()出来的还是以前的内容!!
郁闷啊!!