python模块学习---HTMLParser(解析HTML文档元素)
2017-04-21 17:26
701 查看
HTMLParser是Python自带的模块,使用简单,能够很容易的实现HTML文件的分析。
本文主要简单讲一下HTMLParser的用法.
使用时需要定义一个从类HTMLParser继承的类,重定义函数:
handle_starttag( tag, attrs)
handle_startendtag( tag, attrs)
handle_endtag( tag)
来实现自己需要的功能。
tag是的html标签,attrs是 (属性,值)元组(tuple)的列表(list)。
HTMLParser自动将tag和attrs都转为小写。
下面给出的例子抽取了html中的所有链接:(在PYTHON3.3版本中)
from html.parser import HTMLParser
class MyHTMLParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.links = []
def handle_starttag(self, tag, attrs):
#print "Encountered the beginning of a %s tag" % tag
if tag == "a":
if len(attrs) == 0:
pass
else:
for (variable, value) in attrs:
if variable == "href":
self.links.append(value)
if __name__ == "__main__":
html_code = """ <a href="www.google.com"> google.com</a> <A Href="www.pythonclub.org"> PythonClub </a> <A HREF = "www.sina.com.cn"> Sina </a> """
hp = MyHTMLParser()
hp.feed(html_code)
hp.close()
print(hp.links)
运行结果为:
['www.google.com', 'www.pythonclub.org', 'www.sina.com.cn']
---------------------------------------------
显示HTML中<a>标签之间的文字:
from html.parser import HTMLParser
page ='''''<sada>啊啊啊</sada><a href="http://click.union.360buy.com/JdClick /?unionId=75" class="f1" style="padding-left:13px; padding-right:14px">京东商城</a></td><td><a href="http://www.letao.com /?source=hao123" class="f1">乐淘网上鞋城</a></td><td><a href="http://www.lashou.com/cl_today/w_3001" class="f2">拉手团购</a></td><td><a href="http://www.amazon.cn/?tag=2009hao123famousdaohang" class="f2">亚马逊</a></td><td><a href="http://www.vancl.com/?source=hao123mp" class="f1">凡客诚品</a></td><td><a href="http://reg.jiayuan.com/st/?id=3237&url=/st /main.php" class="f1">世纪佳缘'''
class hp(HTMLParser):
a_text = False
def handle_starttag(self,tag,attr):
if tag == 'a':
self.a_text = True
#print (dict(attr))
def handle_endtag(self,tag):
if tag == 'a':
self.a_text = False
def handle_data(self,data):
if self.a_text:
print (data)
yk = hp()
yk.feed(page)
yk.close()
运行结果如下:
京东商城
乐淘网上鞋城
拉手团购
亚马逊
凡客诚品
世纪佳缘
注:在eclipse中的pydev中调试,记得中文编码问题,在项目中右键改编码为utf-8
本文主要简单讲一下HTMLParser的用法.
使用时需要定义一个从类HTMLParser继承的类,重定义函数:
handle_starttag( tag, attrs)
handle_startendtag( tag, attrs)
handle_endtag( tag)
来实现自己需要的功能。
tag是的html标签,attrs是 (属性,值)元组(tuple)的列表(list)。
HTMLParser自动将tag和attrs都转为小写。
下面给出的例子抽取了html中的所有链接:(在PYTHON3.3版本中)
from html.parser import HTMLParser
class MyHTMLParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.links = []
def handle_starttag(self, tag, attrs):
#print "Encountered the beginning of a %s tag" % tag
if tag == "a":
if len(attrs) == 0:
pass
else:
for (variable, value) in attrs:
if variable == "href":
self.links.append(value)
if __name__ == "__main__":
html_code = """ <a href="www.google.com"> google.com</a> <A Href="www.pythonclub.org"> PythonClub </a> <A HREF = "www.sina.com.cn"> Sina </a> """
hp = MyHTMLParser()
hp.feed(html_code)
hp.close()
print(hp.links)
运行结果为:
['www.google.com', 'www.pythonclub.org', 'www.sina.com.cn']
---------------------------------------------
显示HTML中<a>标签之间的文字:
from html.parser import HTMLParser
page ='''''<sada>啊啊啊</sada><a href="http://click.union.360buy.com/JdClick /?unionId=75" class="f1" style="padding-left:13px; padding-right:14px">京东商城</a></td><td><a href="http://www.letao.com /?source=hao123" class="f1">乐淘网上鞋城</a></td><td><a href="http://www.lashou.com/cl_today/w_3001" class="f2">拉手团购</a></td><td><a href="http://www.amazon.cn/?tag=2009hao123famousdaohang" class="f2">亚马逊</a></td><td><a href="http://www.vancl.com/?source=hao123mp" class="f1">凡客诚品</a></td><td><a href="http://reg.jiayuan.com/st/?id=3237&url=/st /main.php" class="f1">世纪佳缘'''
class hp(HTMLParser):
a_text = False
def handle_starttag(self,tag,attr):
if tag == 'a':
self.a_text = True
#print (dict(attr))
def handle_endtag(self,tag):
if tag == 'a':
self.a_text = False
def handle_data(self,data):
if self.a_text:
print (data)
yk = hp()
yk.feed(page)
yk.close()
运行结果如下:
京东商城
乐淘网上鞋城
拉手团购
亚马逊
凡客诚品
世纪佳缘
注:在eclipse中的pydev中调试,记得中文编码问题,在项目中右键改编码为utf-8
相关文章推荐
- python模块学习---HTMLParser(解析HTML文档元素)
- python模块学习---HTMLParser(解析HTML文档元素)
- python模块学习---HTMLParser(解析HTML文档元素)
- [转载]python模块学习---HTMLParser(解析HTML文档元素)
- python模块学习---HTMLParser(解析HTML文档元素)
- python 解析html文档模块HTMLPaeser
- python网络编程学习笔记(七):HTML和XHTML解析(HTMLParser、BeautifulSoup)
- 使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies .
- 【python】使用HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies
- python网络编程学习笔记(7):HTML和XHTML解析(HTMLParser、BeautifulSoup)
- python网络编程学习笔记(7):HTML和XHTML解析(HTMLParser、BeautifulSoup)
- python开发_HTMLParser_html文档解析
- Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)
- python 模块-使用HTMLParser 解析html
- python学习笔记三:解析html(HTMLParser、SGMLParser),并抓取图片
- Python中使用HTMLParser解析HTML文档
- 使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)(转)
- 黄聪:使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)
- 黄聪:使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)
- 使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies