Reference Manual | Prev: Chapter 14. W4 Web Walker | Next: Chapter 16. Lambda-IR Information Retrieval
The CL-HTTP HTML Parser (version 12) is a DTD-driven parser that reads HTML documents into structured parse trees of CLOS objects. It supports HTML 2.0, HTML 3.2, HTML 4.0 (strict, transitional, and frameset), and HTML5. The parser uses metadata from the loaded DTD to determine element containment, implicit open/close tags, and content models, producing a correct parse tree even for malformed HTML.
The parser lives in the :HTML-PARSER package. When
loaded within CL-HTTP (indicated by the :cl-http feature),
it integrates with CL-HTTP's tokenizer (:TK1), property
list mixin, and JSON serialization via :ST-JSON. It can
also be used standalone with its own tokenizer and property list
implementations.
Key capabilities:
abstract-tag-instance objects with
attributes and child parts
The primary parsing interface is parse-document, a
generic function that retrieves and parses an HTML resource, returning
a parsed-document instance that captures both the parse
tree and retrieval metadata.
;; Parse a local HTML file
(html-parser:parse-document #p"/path/to/page.html")
=> #<PARSED-DOCUMENT /path/to/page.html 47 tags 3ms>
;; Parse with a specific DTD and retain the raw source
(html-parser:parse-document #p"/path/to/page.html"
:dtd "HTML5"
:retain-source t)
;; Parse a remote page (requires CL-HTTP client) (html-parser:parse-document "http://www.example.com/page.html") => #<PARSED-DOCUMENT http://www.example.com/page.html 123 tags 45ms>
;; Low-level: parse an HTML string directly (html-parser:initialize-parser "HTML5") (html-parser:simple-parser "<html><body><p>Hello</p></body></html>")
parsed-document ClassA parsed-document object captures both the parse result
and retrieval metadata:
(html-parser:document-url doc)
(html-parser:document-scheme doc)
:http, :https,
:file, or :ftp.
(html-parser:document-access-time doc)
(html-parser:document-content-type doc)
(html-parser:document-status-code doc)
nil for file/ftp access.
(html-parser:document-parse-tree doc)
abstract-tag-instance of the parse tree.
(html-parser:document-parse-time-msec doc)
(html-parser:document-tag-count doc)
(html-parser:document-source doc)
nil if not retained.
(html-parser:document-dtd doc)
The parse tree consists of nested abstract-tag-instance
objects. Each tag instance has a name, attributes, and child parts
(which may be other tag instances or strings representing PCDATA).
(html-parser:name tag)
P,
DIV, A).
(html-parser:attr-values tag)
(html-parser:parts tag)
(html-parser:part-of tag)
(html-parser:instance-of tag)
(html-parser:html-fragment tag)
The DOM registry manages a collection of parsed documents by name, enabling retrieval and release:
(html-parser:parse-and-register url name &key dtd)
(html-parser:find-document name)
(html-parser:release-document name)
(html-parser:list-documents)
(html-parser:document-title doc)
(html-parser:document-outline doc)
(html-parser:document-links doc)
(html-parser:document-text doc)
(html-parser:document-tree doc)
(html-parser:document-find doc tag-name)
(html-parser:count-parse-tree-tags node)
The parser uses compiled DTD definitions that specify element containment rules, attribute definitions, entity declarations, and content models. The DTD determines how the parser handles implicit open/close tags, unknown elements, and validation.
The variable html-parser:*html-dtd-list* lists the
available DTDs:
| Doctype Identifier | Description |
|---|---|
"HTML5" | HTML5 (default) — extends HTML 4.0 Transitional with HTML5 sectioning, grouping, inline, and media elements |
"-//W3C//DTD HTML 4.0 Transitional//EN" | HTML 4.0 Transitional |
"-//W3C//DTD HTML 4.0 Frameset//EN" | HTML 4.0 Frameset |
"-//W3C//DTD HTML 4.0//EN" | HTML 4.0 Strict |
"-//W3C//DTD HTML 3.2 Draft//EN" | HTML 3.2 |
"-//IETF//DTD HTML 2.0//EN" | HTML 2.0 |
;; Initialize with the default DTD (HTML5) (html-parser:initialize-parser) ;; Initialize with a specific DTD (html-parser:initialize-parser "-//W3C//DTD HTML 4.0//EN")
initialize-parser compiles and loads the DTD if needed,
sets up tag containment backpointers, defines the unknown-tag handler,
and initializes the parse dispatch functions. The current DTD is stored
in html-parser:*current-dtd*.
The HTML5 DTD extends HTML 4.0 Transitional with these element groups:
section, article,
aside, nav, header,
footer, main
figure, figcaption,
details, summary, dialog,
hgroup
mark, time,
data, wbr, output,
meter, progress
video, audio,
canvas, picture
template, slot,
embed, svg, math,
datalist, source, track
;; Define a new entity group (html-parser:define-html-entity #e"%my-elements" (:or custom-widget data-grid)) ;; Define a custom element (html-parser:define-html-element custom-widget :content (* #e"%flow") :attributes (#e"%attrs")) ;; Register a new DTD (html-parser:modify-dtd-list "My-DTD" "my-dtd-file")
The HTML parser integrates with W4 for web scraping workflows. Parse documents retrieved by the walker, extract structure, and apply constraints based on HTML content:
;; Walk a site, parsing each page
(w4:with-activity ("scrape"
(:report-stream '*standard-output*)
:constraints '((depth 2)
(no-cycles)
(header-content-type (:text :html)))
:actions '((trace) (generate-inferiors)))
;; Within a custom action, parse retrieved content:
;; (html-parser:parse-document url)
(walk "http://www.example.com/" activity))
When CL-HTTP is loaded with ST-JSON support, parsed documents can be serialized to JSON for consumption by language models:
(html-parser:document-to-lisp doc)
(html-parser:document-to-html doc)
(html-parser:write-document-source doc stream)
(html-parser:validate-and-register url name)
html-parser:*validation-messages*.
html-parser:*current-dtd*
nil.
html-parser:*dtd-tags*
html-parser:*dtd-toplevel-tags*