您的位置:首页 > 编程语言 > Python开发

Python之HTML的解析(网页抓取一) 分类: HTMLParser 2013-11-08 20:32 5203人阅读 评论(0) 收藏

2013-11-08 20:32 435 查看
对html的解析是网页抓取的基础,分析抓取的结果找到自己想要的内容或标签以达到抓取的目的。

HTMLParser是python用来解析html的模块。它可以分析出html里面的标签、数据等等,是一种处理html的简便途径。 HTMLParser采用的是一种事件驱动的模式,当HTMLParser找到一个特定的标记时,它会去调用一个用户定义的函数,以此来通知程序处理它主要的用户回调函数的命名都是以handler_开头的,都是HTMLParser的成员函数。当我们使用时,就从HTMLParser派生出新的类,然后重新定义这几个以handler_开头的函数即可。这几个函数包括:

handle_startendtag 处理开始标签和结束标签
handle_starttag 处理开始标签,比如<xx> tag不区分大小写

handle_endtag 处理结束标签,比如</xx>
handle_charref 处理特殊字符串,就是以&#开头的,一般是内码表示的字符

handle_entityref 处理一些特殊字符,以&开头的,比如  

handle_data 处理数据,就是<xx>data</xx>中间的那些数据

handle_comment 处理注释
handle_decl 处理<!开头的,比如<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
handle_pi 处理形如<?instruction>的东西

def handle_starttag(self,tag,attr):

#注意:tag不区分大小写,此时也可以解析 <A 标签


# SGMLParser 会在创建attrs 时将属性名转化为小写。



if tag=='a':

for href,link in attr:

if href.lower()=="href":


pass



1. 基本解析,找到开始和结束标签

#coding:utf-8

from HTMLParser import HTMLParser
'''
HTMLParser的成员函数:

handle_startendtag  处理开始标签和结束标签
handle_starttag     处理开始标签,比如<xx>
handle_endtag       处理结束标签,比如</xx>
handle_charref      处理特殊字符串,就是以&#开头的,一般是内码表示的字符
handle_entityref    处理一些特殊字符,以&开头的,比如
handle_data         处理数据,就是<xx>data</xx>中间的那些数据
handle_comment      处理注释
handle_decl         处理<!开头的,比如<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
handle_pi           处理形如<?instruction>的东西

'''
class myHtmlParser(HTMLParser):
#处理<!开头的内容
def handle_decl(self,decl):
print 'Encounter some declaration:'+ decl
def handle_starttag(self,tag,attrs):
print 'Encounter the beginning of a %s tag' % tag
def handle_endtag(self,tag):
print 'Encounter the end of a %s tag' % tag
#处理注释
def handle_comment(self,comment):
print 'Encounter some comments:' + comment

if __name__=='__main__':
a = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\
<html><head><!--insert javaScript here!--><title>test</title><body><a href="http: //www.163.com">链接到163</a></body></html>'
m=myHtmlParser()
m.feed(a)
m.close()

输出结果:

Encounter some declaration:DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
Encounter the beginning of a html tag
Encounter the beginning of a head tag
Encounter some comments:insert javaScript here!
Encounter the beginning of a title tag
Encounter the end of a title tag
Encounter the beginning of a body tag
Encounter the beginning of a a tag
Encounter the end of a a tag
Encounter the end of a body tag
Encounter the end of a html tag


2. 解析html的超链接和链接显示的内容


#coding:utf-8
from HTMLParser import HTMLParser
class myHtmlParser(HTMLParser):

def __init__(self):
HTMLParser.__init__(self)
self.flag=None

# 这里重新定义了处理开始标签的函数
def handle_starttag(self,tag,attrs):
# 判断标签<a>的属性
if tag=='a':
self.flag='a'
for href,link in attrs:
if href=='href':
print "href:",link

def handle_data(self,data):
if self.flag=='a':
print "data:",data.decode('utf-8')

if __name__ == '__main__':
a = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\
<html><head><!--insert javaScript here!--><title>test</title><body><a href="http: //www.163.com">链接到163</a></body></html>'
m=myHtmlParser()
m.feed(a)
m.close()

输出结果:

href: http: //www.163.com
data: 链接到163


或:

#coding:utf-8

from  HTMLParser import HTMLParser
import urllib2

class myparser(HTMLParser):

# 继承父类初始化方法,并添加一个tag属性
def __init__(self):
HTMLParser.__init__(self)
self.tag = None

def handle_decl(self,decl):
print u"声明:",decl

def handle_starttag(self,tag,attrs):
print u"开始标签;",tag

# 判断是否是a开头的标签
if tag=='a' and len(attrs):
#设置 self.tag 标记
self.tag='a'
for href,link in attrs:
if href=='href':
print href+":"+link

def handle_endtag(self,tag):
print u"结束标签:",tag

def handle_data(self,data):
#处理 a 标签开头的数据
if self.tag=='a':
print u"数据内容:",data.decode("utf-8")

def handle_comment(self,comm):
print u"注释:",comm

if __name__ == '__main__':

a = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\
<html><head><!--insert javaScript here!--><title>test</title><body><a href="http: //www.163.com">链接到163</a><a href="http: //www.baidu.com">百度</a></body></html>'
m = myparser()
m.feed(a)

结果:

声明: DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
开始标签; html
开始标签; head
注释: insert javaScript here!
开始标签; title
结束标签: title
开始标签; body
开始标签; a
href:http: //www.163.com
数据内容: 链接到163
结束标签: a
开始标签; a
href:http: //www.baidu.com
数据内容: 百度
结束标签: a
结束标签: body
结束标签: html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐