html.parse(page)the parsed page, doc, which has the next three calls
doc.find(selector)the first element that matches, or null
doc.all(selector)every element that matches, in the page's order
doc.tree()the page's root element
html.text(page)the page's text, a line for each block
page is text, bytes or a blob of at most 64 MiB. Bytes and a blob are decoded from the character set that a byte order mark, a blob's type or the page's meta tag names.
Any page parses, as in a browser: html, head and body are added when the page omits them, and an element left open is closed. No script on the page runs. html.text omits script, style, noscript, template and head.
An element is { tag, attrs, text, children }.
tagits name, in lower case
attrsa map of its attributes
textall the text inside it
childrenthe elements directly inside it, each an element
A selector is one of these.
tdan element of that name
*any element
#mainthe element with that id
.pricean element with that word in its class
[href]an element that has the attribute
[type=text]an element whose attribute has that value
a.external[href]an element that matches every part
table tra tr anywhere inside a table
Errors:
bad_input a selector of any other form, such as one with > or a comma
Needs nothing.
These calls parse text.
html.parse("<title>T</title>").find("title").text // gives "T"
html.parse("<title>T</title>").tree().tag // gives "html"
html.text("<h1>News</h1><p>Tea is up.</p>") // gives "News\nTea is up."This script parses a page fetched with http.
allow: [net:example.com]
const doc = html.parse(http.get("https://example.com/news").body)
return doc.all("a[href]").map((a) => ({ text: a.text.trim(), href: a.attrs.href }))
// gives [{ text: "Tea is up", href: "/news/tea" }, { text: "Cake is down", href: "/news/cake" }]