Getting started
Here’s the smallest useful thing Parsec does: take a page, parse it the way a browser would, and pull out the elements you want with a CSS selector. Each tab below runs as written and prints the same two lines.
cargo add parsec-webuse parsec::css::SelectorList;
fn main() { let doc = parsec::parse( r#"<ul> <li class="book"><a href="/p/1">Dune</a></li> <li class="book sold"><a href="/p/2">Emma</a></li> <li class="book"><a href="/p/3">Ulysses</a></li></ul>"#, false, ); let in_stock = SelectorList::parse("li.book:not(.sold) a").unwrap(); for a in in_stock.select(&doc, doc.root()) { let href = doc.attrs(a).find(|&(_, name, _)| name == "href").map(|(_, _, value)| value); println!("{} {}", doc.text(a), href.unwrap_or("")); }}The package is parsec-web, and the library inside it is parsec, so your code says use parsec::…. The false is for scripting: pass true only if you want <noscript> read the way a browser with JavaScript on reads it.
pip install parsec-webfrom parsec_html import HTMLParser
page = HTMLParser("""<ul> <li class="book"><a href="/p/1">Dune</a></li> <li class="book sold"><a href="/p/2">Emma</a></li> <li class="book"><a href="/p/3">Ulysses</a></li></ul>""")
for a in page.css("li.book:not(.sold) a"): print(a.text(), a.attributes["href"])You install parsec-web and import parsec_html. There’s already a parsec on PyPI, a parser-combinator library, and two packages that install the same module name overwrite each other. The API is selectolax’s, so if you’re coming from selectolax, most of your code only needs the import changed.
npm install parsec-webimport { parse } from "parsec-web";
const doc = parse(`<ul> <li class="book"><a href="/p/1">Dune</a></li> <li class="book sold"><a href="/p/2">Emma</a></li> <li class="book"><a href="/p/3">Ulysses</a></li></ul>`);
for (const a of doc.select("li.book:not(.sold) a")) { console.log(a.textContent, a.getAttribute("href"));}In Node you get a native build for your platform, with WebAssembly as the fallback; in a browser you get the WebAssembly. The API is the same either way, and node names follow the DOM’s.
All three print:
Dune /p/1Ulysses /p/3Notice what you didn’t have to write. There’s no <html>, <head> or <body> in that markup, and it doesn’t matter: Parsec adds them where the HTML Standard says they go, the same as a browser. It never fails on bad markup, because the standard says how to recover from every mistake, and Parsec follows it.
From C, the library is a header and a static or shared library for each platform; install says where to get them.