Chapter 23. Structured Text: HTML
Most documents on the Web use HTML, the HyperText Markup Language. Markup is the insertion of special tokens, known as tags, in a text document to give structure to the text. HTML is, in theory, an application of the large, general standard known as SGML, the Standard General Markup Language. In practice, many of the Web’s documents use HTML in sloppy or incorrect ways. Browsers have evolved many practical heuristics over the years to try and compensate for this, but even so, it still sometimes happens that a browser displays an incorrect web page in some weird way (don’t blame the browser: 9 times out of 10, all the blame and then some is deserved by the web page’s author!).
HTML is not suitable for much more than presenting documents on a screen. Complete and precise extraction of the information in the document, working backward from the document’s presentation, is often unfeasible. To tighten things up, HTML has evolved into a more rigorous standard called XHTML. XHTML is very similar to traditional HTML, but it is defined in terms of XML and more precisely than HTML. You can handle XHTML with the tools covered in Chapter 24.
Despite the difficulties, it’s often possible to extract at least some useful information from HTML documents (a task often known as screen-scraping, or just scraping). Python supplies the sgmllib
, htmllib
, and HTMLParser
modules for the task of parsing HTML documents, whether this parsing is for the purpose of presenting ...
Get Python in a Nutshell, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.