在Python中使用HTMLParser解析HTML的教程


Posted in Python onApril 29, 2015

如果我们要编写一个搜索引擎,第一步是用爬虫把目标网站的页面抓下来,第二步就是解析该HTML页面,看看里面的内容到底是新闻、图片还是视频。

假设第一步已经完成了,第二步应该如何解析HTML呢?

HTML本质上是XML的子集,但是HTML的语法没有XML那么严格,所以不能用标准的DOM或SAX来解析HTML。

好在Python提供了HTMLParser来非常方便地解析HTML,只需简单几行代码:

from HTMLParser import HTMLParser
from htmlentitydefs import name2codepoint

class MyHTMLParser(HTMLParser):

  def handle_starttag(self, tag, attrs):
    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('<!-- -->')

  def handle_entityref(self, name):
    print('&%s;' % name)

  def handle_charref(self, name):
    print('&#%s;' % name)

parser = MyHTMLParser()
parser.feed('<html><head></head><body><p>Some <a href=\"#\">html</a> tutorial...<br>END</p></body></html>')

feed()方法可以多次调用,也就是不一定一次把整个HTML字符串都塞进去,可以一部分一部分塞进去。

特殊字符有两种,一种是英文表示的 ,一种是数字表示的Ӓ,这两种字符都可以通过Parser解析出来。
小结

找一个网页,例如https://www.python.org/events/python-events/,用浏览器查看源码并复制,然后尝试解析一下HTML,输出Python官网发布的会议时间、名称和地点。

Python 相关文章推荐
python按综合、销量排序抓取100页的淘宝商品列表信息
Feb 24 Python
Python实现基于POS算法的区块链
Aug 07 Python
Linux下python3.6.1环境配置教程
Sep 26 Python
python使用phoenixdb操作hbase的方法示例
Feb 28 Python
ORM Django 终端打印 SQL 语句实现解析
Aug 09 Python
如何在Python 游戏中模拟引力
Mar 27 Python
python matplotlib模块基本图形绘制方法小结【直线,曲线,直方图,饼图等】
Apr 26 Python
python读取hdfs上的parquet文件方式
Jun 06 Python
关于pycharm 切换 python3.9 报错 ‘HTMLParser‘ object has no attribute ‘unescape‘ 的问题
Nov 24 Python
简述python四种分词工具,盘点哪个更好用?
Apr 13 Python
Python IO文件管理的具体使用
Mar 20 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Apr 09 Python
python安装以及IDE的配置教程
Apr 29 #Python
python获取从命令行输入数字的方法
Apr 29 #Python
在Python中处理XML的教程
Apr 29 #Python
python搜索指定目录的方法
Apr 29 #Python
python中sleep函数用法实例分析
Apr 29 #Python
介绍Python中内置的itertools模块
Apr 29 #Python
python使用fileinput模块实现逐行读取文件的方法
Apr 29 #Python
You might like
PHP7.1新功能之Nullable Type用法分析
2016/09/26 PHP
js 键盘记录实现(兼容FireFox和IE)
2010/02/07 Javascript
js function使用心得
2010/05/10 Javascript
javascript getElementsByClassName实现代码
2010/10/11 Javascript
Jquery网页出现的乱码问题的三种解决方法
2013/06/30 Javascript
Jjcarousellite 实现图片列表滚动的简单实例
2013/11/29 Javascript
ExtJS4如何给同一个formpanel不同的url
2014/05/02 Javascript
js判断浏览器版本以及浏览器内核的方法
2015/01/20 Javascript
浅谈Javascript中substr和substring的区别
2015/09/30 Javascript
window.location.reload 刷新使用分析(去对话框)
2015/11/11 Javascript
JQuery控制图片由中心点逐渐放大效果
2016/06/26 Javascript
深入浅析JS Function()构造函数
2016/08/22 Javascript
谈谈VUE种methods watch和compute的区别和联系
2017/08/01 Javascript
vuejs 动态添加input框的实例讲解
2018/08/24 Javascript
利用Bootstrap Multiselect实现下拉框多选功能
2019/04/08 Javascript
Vue+ElementUI项目使用webpack输出MPA的方法
2019/08/27 Javascript
vue 通过 Prop 向子组件传递数据的实现方法
2020/10/30 Javascript
[59:35]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#1COL VS Alliance第二局
2016/03/04 DOTA
Python实现压缩和解压缩ZIP文件的方法分析
2017/09/28 Python
Python多层装饰器用法实例分析
2018/02/09 Python
Python即时网络爬虫项目启动说明详解
2018/02/23 Python
Django 浅谈根据配置生成SQL语句的问题
2018/05/29 Python
python爬取网页转换为PDF文件
2018/06/07 Python
Python中如何使用if语句处理列表实例代码
2019/02/24 Python
python assert的用处示例详解
2019/04/01 Python
python3.6 如何将list存入txt后再读出list的方法
2019/07/02 Python
详解如何在cmd命令窗口中搭建简单的python开发环境
2019/08/29 Python
python中的 zip函数详解及用法举例
2020/02/16 Python
python 基于opencv去除图片阴影
2021/01/26 Python
英国网上购买门:Direct Doors
2018/06/07 全球购物
蛋糕店的商业计划书范文
2014/01/27 职场文书
企业理念标语
2014/06/09 职场文书
国庆节标语大全
2014/10/08 职场文书
2015关于重阳节的演讲稿
2015/03/20 职场文书
小学思想品德教学反思
2016/02/24 职场文书
Elasticsearch 基本查询和组合查询
2022/04/19 Python