8.2.sgmllib.py介绍
HTML处理分成三步:将HTML分解成它的组成片段,对片段进行加工,接着将片段再重新合成 HTML。第一步是通过sgmllib.py来完成的,它是标准Python库的一部分。
理解本章的关键是要知道HTML不只是文本,更是结构化文本。这种结构来源于开始与结束标记的或多或少分级序列。通常您并不以这种方式处理HTML,而是以文本方式在一个文本编辑中对其进行处理,或以可视的方式在一个浏览器中进行浏览或页面编辑工具中进行编辑。sgmllib.py表现出了HTML的结构。
sgmllib.py包含一个重要的类:SGMLParser。SGMLParser将HTML分解成有用的片段,比如开始标记和结束标记。在它成功地分解出某个数据为一个有用的片段后,它会根据所发现的数据,调用一个自身内部的方法。为了使用这个分析器,您需要子类化SGMLParser类,并且覆盖这些方法。这就是当我说它表示了HTML结构的意思:HTML的结构决定了方法调用的次序和传给每个方法的参数。
SGMLParser将HTML分析成 8 类数据,然后对每一类调用单独的方法:
Python2.0 存在一个 bug,即SGMLParser完全不能识别声明 (handle_decl永远不会调用),这就意味着DOCTYPE被静静地忽略掉了。这个错误在Python2.1 中改正了。 |
sgmllib.py所附带的一个测试套件举例说明了这一点。您可以运行sgmllib.py,在命令行下传入一个HTML文件的名字,然后它会在分析标记和其它元素的同时将它们打印出来。它的实现是通过子类化SGMLParser类,然后定义unknown_starttag,unknown_endtag,handle_data和其它方法来实现的。这些方法简单地打印出它们的参数。
在 Windows 下的ActivePythonIDE中,您可以在 “Run script” 对话框中指定命令行参数。用空格将多个参数分开。 |
例8.4.sgmllib.py的样例测试
下面是一个片段,来自本书的HTML版本的目录,toc.html。当然,您的存储路径可能与我的有所不同。 (如果您还没有下载本书的HTML版本,可以从http://diveintopython.org/下载。
c:\python23\lib> type "c:\downloads\diveintopython\html\toc\index.html"
<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Dive Into Python</title>
<link rel="stylesheet" href="diveintopython.css" type="text/css">
... 略 ...
通过sgmllib.py的测试套件来运行它,会得到如下的输出结果:
c:\python23\lib> python sgmllib.py "c:\downloads\diveintopython\html\toc\index.html" data: '\n\n' start tag: <html lang="en" > data: '\n ' start tag: <head> data: '\n ' start tag: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" > data: '\n \n ' start tag: <title> data: 'Dive Into Python' end tag: </title> data: '\n ' start tag: <link rel="stylesheet" href="diveintopython.css" type="text/css" > data: '\n ' ... 略 ...
下面是本章其它部分的路标:
- 子类化SGMLParser来创建从HTML文档中抽取感兴趣的数据的类。
- 子类化SGMLParser来创建BaseHTMLProcessor,它覆盖了所有8个处理方法,然后使用它们从片段中重建原始的HTML。
- 子类化BaseHTMLProcessor来创建Dialectizer,它增加了一些方法,专门用来处理指定的HTML标记,然后覆盖了handle_data方法,提供了用来处理HTML标记之间文本块的框架。
- 子类化Dialectizer来创建定义了文本处理规则的类。这些规则被Dialectizer.handle_data使用。
- 编写一个测试套件,它可以从http://diveintopython.org/处抓取一个真正的 web 页面,然后处理它。
继续阅读本章,您还可以学习到有关locals、globals和基于 dictionary 的字符串格式化的内容。
相关推荐
00_Introduction.rar00_Introduction.rar00_Introduction.rar00_Introduction.rar00_Introduction.rar00_Introduction.rar00_Introduction.rar00_Introduction.rar00_Introduction.rar00_Introduction.rar00_...
1.1 Introduction 1 1.2 Information Warfare 1 1.3 Electronic Warfare 3 1.3.1 Electronic Support 3 1.3.2 Electronic Attack 4 1.3.3 Electronic Protect 4 1.4 Electronic Support 5 1.4.1 Low Probability of ...
Introduction to Computing Systems from Bits and Gates to C and Beyond.pdf 2nd Edition [English] 本书是计算机系统概论的英文原版,计算机科学的经典基础教材。全书以自底向上方法帮助学生理解计算机系统的原理...
Reinforcement learning an introduction中文pdf
Introduction to Deep Learning From Logical Calculus to Artificial Intelligence
Introduction to Wireless and Mobile Systems
Introduction to Classical Mechanics
Density Functional Theory: A Practical Introduction offers a concise, easy-to-follow introduction to the key concepts and practical applications of DFT, focusing on plane-wave DFT. The authors have ...
A Mathematical Introduction to Robotic Manipulation Richard M. Murray, Zexiang Li and S. Shankar Sastry
Basic introduction to computational fluid dynamics, using the finite volume method. Table of Contents Preface Acknowledgements 1 Introduction 1 2 Conservation Laws of Fluid Motion and ...
Hack Audio:An Introduction to Computer Programming and Digital Signal Processing in Matlab 2019 Hack Audio:An Introduction to Computer Programming and DSP in Matlab.part1.rar (15 MB, 下载次数: 237...
【书名】 An Introduction to Copulas 【作者】Nelsen, Roger B. 【出版社】Springer【版本】2nd ed【出版日期】2006【文件格式】PDF【文件大小】2.28 MB;【页数】250 Pages;【ISBN出版号】ISBN: 978-0-387-28659-4;...
MIMO的2014年最新英文原版书籍,Introduction to MIMO Communications
The Introduction of Pump The Introduction of Pump The Introduction of Pump The Introduction of Pump
This book gives an excellent introduction to fluid dynamics First published in 1967, Professor Batchelor's classic work is still one of the foremost texts on fluid dynamics. His careful presentation ...
This book is an accessible introduction to the theory of space-time wireless communications. The authors discuss the basics of space-time propagation, space-time channels, channel capacity, spatial ...
Introduction to Modern Cryptography
An Introduction to the Bootstrap
An Introduction to Numerical Analysis Endre S¨uli and David F. Mayers University of Oxford Cambridge University Press Contents 1 Solution of equations by iteration 2 Solution of systems of linear ...
An Introduction to Manifolds, by Loring W. Tu.