HTMLParser 解析HTML
2016-01-12 20:00
330 查看
from html.parser import HTMLParser from html.entities import name2codepoint class MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): for (variable, value) in attrs: print(variable, value) if variable == 'class' and value == 'item': print(attrs) break print('<%s>' % tag) def handle_endtag(self, tag): print('</%s>' % tag) def handle_startendtag(self, tag, attrs): print('<%s/>' % tag) def handle_data(self, data): print(data) def handle_comment(self, data): print('<!--', data, '-->') def handle_entityref(self, name): print('&%s;' % name) def handle_charref(self, name): print('%s;' % name) parser = MyHTMLParser() parser.feed('''<html> <head></head> <body> <!-- test html parser --> <p class=\"item\" id=\"item1\">Some <a href=\"#\">html</a> HTML tutorial...<br>END</p> </body></html>''')
相关文章推荐
- HTML学习笔记
- 如何让html输入域中只填写英文,不能填写中文
- HTML 部分名词解释(CDN、DOM)
- 关于mvc中@Html.DropDownListFor和@Html.DropDownList默认值无法选中问题简单总结
- 1月12日,HTML学习笔记2
- html中不要忽略一些细节
- <!DOCTYPE html>
- 20160112html学习笔记表格列表
- htm、html、shtml区别
- window.location.href和window.location.replace的区别
- HTML <area> 标签 带有可点击区域的图像映射(图像映射指的是带有可点击区域的图像)
- Convert HTML to PDF with New Plugin
- CSDN博客专栏HTML语言编写详解
- html导出table支持(excel,png,word,pdf<pdf不太行,中文不支持>)
- html中class和id的区别
- html的meta总结,html标签中meta属性使用介绍
- html中使用iframe时后面的内容不显示
- C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子)
- 1月11日,HTML学习笔记
- 【HTML】表单form中一单回车就提交的text、一点击就提交的button