Moving from html5ever, scraper or kuchikiki
html5ever and Parsec both follow the HTML Standard, so on the same page they build the same tree. What changes when you move is how you hold that tree. scraper and kuchikiki hand you element references that borrow from, or count references into, a tree of nodes. Parsec hands you a Document and numbers for its nodes, and you do everything through the document.
Every example on this page compiles and runs as written: the “before” half against scraper 0.29.0, kuchikiki 0.8.2, html5ever 0.40.1 and markup5ever_rcdom 0.40.1, the “after” half against parsec-web 0.3.1.
Should you move?
Section titled “Should you move?”Reasons to:
- Speed. Rust against Rust, in one process, in the run of 29 November 2026 on an Apple M2 Max (parsec.tools/benchmarks): over 13 real pages (24 MB of Wikipedia, MDN, GitHub, the BBC and others), Parsec parsed 362 MB/s and html5ever, building
markup5ever_rcdom’s tree, 83. Over 1,746 pages from a real crawl, 563 MB, Parsec took 0.56 s and html5ever 3.40. - Threads. A Parsec
DocumentisSendandSync, so many threads can read and query one page at once. scraper’sHtmlisn’tSendunless you turn on itsatomicfeature, and isn’tSynceven then. kuchikiki’s nodes areRcs and stay on one thread. - One crate for the rest. Selectors, editing and serialization are in
parsec-web; XPath 1.0 is inparsec-xpath; bytes in any encoding, sniffed as a browser sniffs them, are inparsec-web-encoding. With html5ever you put those together from several crates, some (selectors,cssparser) at versions you have to match. - Edits that can’t corrupt the tree.
appendandinsert_beforerefuse to put a node inside itself, and a removed node’s id reads as nothing rather than dangling.
Reasons to stay:
- Memory. In the same run html5ever’s
RcDomwas smaller than Parsec’s tree on 9 of the 13 pages, and over the 1,746 crawled pages the process peaked at 92 MB with html5ever against 101 MB with Parsec. - Your own tree. html5ever builds into any
TreeSink, so you can parse straight into your own structure. Parsec builds its ownDocument, and you read from that. - Servo and the ecosystem. html5ever is Servo’s parser, and scraper has many dependents and years of use. Parsec is newer.
- Ergonomics you may prefer. scraper’s
ElementRefreads naturally (el.value().attr("href"),el.text()), and kuchikiki’sNodeRefcan be edited from anywhere you hold one. In Parsec every read and write goes through&Documentor&mut Document.
What maps to what
Section titled “What maps to what”| scraper / kuchikiki / html5ever | Parsec |
|---|---|
scraper::Html::parse_document(s) |
parsec::parse(s, false) |
kuchikiki::parse_html().one(s) |
parsec::parse(s, false) |
html5ever::parse_document(RcDom::default(), opts).one(s) |
parsec::parse(s, false) |
Html::parse_fragment(s) |
parsec::parse_fragment(s, Ns::Html, "body", false) |
scraper::Selector::parse(s) |
parsec::css::SelectorList::parse(s) |
html.select(&sel), el.select(&sel) |
sel.select(&doc, doc.root()), sel.select(&doc, el) |
doc.select_first(s) (kuchikiki) |
sel.select_first(&doc, doc.root()) |
el.value().attr("href") |
doc.attrs(el).find(|a| a.1 == "href").map(|a| a.2) |
el.value().name() |
doc.lower_name(el), or doc.kind(el) |
el.text().collect::<String>(), text_contents() |
doc.text(el) |
el.html() |
doc.outer_html(el) |
el.inner_html() |
doc.inner_html(el) |
html.html(), doc.to_string() |
doc.inner_html(doc.root()) |
el.parent(), next_sibling(), children() |
doc.parent(el), doc.next_sibling(el), doc.children(el) |
node.detach() (kuchikiki) |
doc.remove(el), or doc.detach(el) to move it |
node.append(child), insert_before |
doc.append(parent, child), doc.insert_before(parent, child, before) |
NodeRef::new_element(QualName, attrs) (kuchikiki) |
doc.create_element(Ns::Html, "li") |
NodeRef::new_text(s) |
doc.create_text(s) |
attributes.borrow_mut().insert(k, v) |
doc.set_attr(el, k, v) |
encoding_rs decode, then parse |
parsec_encoding::parse_bytes(bytes, options) |
TreeSink for your own tree |
not available; read from Document |
API differences
Section titled “API differences”- Nodes are numbers. A
NodeIdis au32index into itsDocument. It’sCopy, it doesn’t borrow, and you can keep it across edits. You pass it back to the document to read:doc.text(id), notel.text(). - Selectors take a scope.
select(&doc, scope)searches underscopeand never returnsscopeitself. Usedoc.root()for the whole page. - Attributes are an iterator.
doc.attrs(el)yields(namespace, name, value)in source order. There’s noattr("x")shortcut yet;findis the idiom. - Edits take
&mut Document. You can’t edit while holding a borrow from a read, so collect the ids first, then edit. Ids stay valid; there’s nothing to re-find. - Errors are values.
SelectorList::parsereturns aResultwith a message. Edits that would break the tree returnEditErrorinstead of panicking.
Side by side
Section titled “Side by side”Read a listing
Section titled “Read a listing”// Before: scraperuse scraper::{Html, Selector};
let html = r#"<ul><li class=item data-sku=A1><a href=/a>Kettle</a><li class=item data-sku=B2><a href=/b>Toaster</a></ul>"#;let doc = Html::parse_document(html);let items = Selector::parse("li.item").unwrap();let link = Selector::parse("a").unwrap();let rows: Vec<(String, String, String)> = doc .select(&items) .map(|li| { let a = li.select(&link).next().unwrap(); ( li.value().attr("data-sku").unwrap().to_string(), a.text().collect(), a.value().attr("href").unwrap().to_string(), ) }) .collect();assert_eq!(rows[1], ("B2".into(), "Toaster".into(), "/b".into()));// After: Parsecuse parsec::css::SelectorList;
let html = r#"<ul><li class=item data-sku=A1><a href=/a>Kettle</a><li class=item data-sku=B2><a href=/b>Toaster</a></ul>"#;let doc = parsec::parse(html, false);let items = SelectorList::parse("li.item").unwrap();let link = SelectorList::parse("a").unwrap();let attr = |el, name: &str| doc.attrs(el).find(|a| a.1 == name).map(|a| a.2.to_string());let rows: Vec<(String, String, String)> = items .select(&doc, doc.root()) .into_iter() .map(|li| { let a = link.select_first(&doc, li).unwrap(); (attr(li, "data-sku").unwrap(), doc.text(a), attr(a, "href").unwrap()) }) .collect();assert_eq!(rows[1], ("B2".into(), "Toaster".into(), "/b".into()));The attr closure is the shortcut scraper has built in. Most code moving over defines one like it.
The same from kuchikiki
Section titled “The same from kuchikiki”// Before: kuchikikiuse kuchikiki::traits::*;
let doc = kuchikiki::parse_html().one(r#"<ul><li data-sku=A1>Kettle<li data-sku=B2>Toaster</ul>"#);let skus: Vec<String> = doc .select("li") .unwrap() .map(|li| li.attributes.borrow().get("data-sku").unwrap().to_string()) .collect();assert_eq!(skus, ["A1", "B2"]);let first = doc.select_first("li").unwrap();assert_eq!(first.text_contents(), "Kettle");// After: Parsecuse parsec::css::SelectorList;
let doc = parsec::parse(r#"<ul><li data-sku=A1>Kettle<li data-sku=B2>Toaster</ul>"#, false);let li = SelectorList::parse("li").unwrap();let skus: Vec<&str> = li .select(&doc, doc.root()) .into_iter() .filter_map(|el| doc.attrs(el).find(|a| a.1 == "data-sku").map(|a| a.2)) .collect();assert_eq!(skus, ["A1", "B2"]);let first = li.select_first(&doc, doc.root()).unwrap();assert_eq!(doc.text(first), "Kettle");Straight from html5ever
Section titled “Straight from html5ever”If you use html5ever with markup5ever_rcdom, you walk Rc<Node>s and match on NodeData:
// Before: html5ever and markup5ever_rcdomuse html5ever::tendril::TendrilSink;use markup5ever_rcdom::{Handle, NodeData, RcDom};
fn links(node: &Handle, out: &mut Vec<String>) { if let NodeData::Element { name, attrs, .. } = &node.data { if &*name.local == "a" { for attr in attrs.borrow().iter() { if &*attr.name.local == "href" { out.push(attr.value.to_string()); } } } } for child in node.children.borrow().iter() { links(child, out); }}
fn main() { let dom = html5ever::parse_document(RcDom::default(), Default::default()) .one(r#"<p><a href=/a>a</a> <a href=/b>b</a>"#); let mut out = Vec::new(); links(&dom.document, &mut out); assert_eq!(out, ["/a", "/b"]);}// After: Parsecuse parsec::css::SelectorList;
let doc = parsec::parse(r#"<p><a href=/a>a</a> <a href=/b>b</a>"#, false);let hrefs: Vec<&str> = SelectorList::parse("a[href]") .unwrap() .select(&doc, doc.root()) .into_iter() .filter_map(|a| doc.attrs(a).find(|x| x.1 == "href").map(|x| x.2)) .collect();assert_eq!(hrefs, ["/a", "/b"]);Walking by hand works the same way in Parsec, with doc.children(id) and doc.kind(id); Parsec from Rust shows it.
Fragments
Section titled “Fragments”// Before: scraperuse scraper::{Html, Selector};
let frag = Html::parse_fragment("<td>1</td><td>2</td>");let tds = Selector::parse("td").unwrap();// Parsed in a <body>, as the HTML Standard says, a <td> on its own is dropped.assert_eq!(frag.select(&tds).count(), 0);// After: Parsecuse parsec::css::SelectorList;use parsec::Ns;
// Name the element the markup will go into, and <td>s are kept.let frag = parsec::parse_fragment("<td>1</td><td>2</td>", Ns::Html, "tr", false);let tds = SelectorList::parse("td").unwrap().select(&frag, frag.root());assert_eq!(tds.len(), 2);assert_eq!(frag.text(tds[1]), "2");scraper always parses a fragment as if it were going into a <body>. Parsec takes the context element, as innerHTML does, so table rows, list items and SVG parse the way they would in place.
Edit and write back out
Section titled “Edit and write back out”// Before: kuchikikiuse kuchikiki::traits::*;use kuchikiki::NodeRef;
let doc = kuchikiki::parse_html().one(r#"<ul><li class=old>a</li></ul>"#);let ul = doc.select_first("ul").unwrap();doc.select_first(".old").unwrap().as_node().detach();// Parsed rather than built, which would need html5ever 0.26's QualName.let li = kuchikiki::parse_html().one("<li class=new></li>").select_first("li").unwrap().as_node().clone();li.detach();li.append(NodeRef::new_text("b & c"));ul.as_node().append(li);let mut out = Vec::new();ul.as_node().serialize(&mut out).unwrap();assert_eq!(String::from_utf8(out).unwrap(), r#"<ul><li class="new">b & c</li></ul>"#);// After: Parsecuse parsec::css::SelectorList;use parsec::Ns;
let mut doc = parsec::parse(r#"<ul><li class=old>a</li></ul>"#, false);let ul = SelectorList::parse("ul").unwrap().select_first(&doc, doc.root()).unwrap();let old = SelectorList::parse(".old").unwrap().select_first(&doc, doc.root()).unwrap();doc.remove(old);let li = doc.create_element(Ns::Html, "li").unwrap();doc.set_attr(li, "class", "new").unwrap();let text = doc.create_text("b & c");doc.append(li, text).unwrap();doc.append(ul, li).unwrap();assert_eq!(doc.outer_html(ul), r#"<ul><li class="new">b & c</li></ul>"#);kuchikiki 0.8.2 is built on html5ever 0.26, so NodeRef::new_element wants that version’s QualName, and html5ever 0.40’s won’t compile there. Parsec names elements with a &str.
Share a page between threads
Section titled “Share a page between threads”// After: Parsecuse parsec::css::SelectorList;use std::thread;
let doc = parsec::parse(&"<p><a href=/1>1</a><a href=/2>2</a></p>".repeat(1000), false);let queries = ["a", "p", "a[href$='1']"];let counts: Vec<usize> = thread::scope(|s| { let handles: Vec<_> = queries .iter() .map(|q| { let doc = &doc; s.spawn(move || SelectorList::parse(q).unwrap().select(doc, doc.root()).len()) }) .collect(); handles.into_iter().map(|h| h.join().unwrap()).collect()});assert_eq!(counts, [2000, 1000, 1000]);With scraper’s default features, &Html can’t cross threads, so this doesn’t compile; each thread parses its own copy.
Bytes in an unknown encoding
Section titled “Bytes in an unknown encoding”html5ever takes text, so you find the encoding and decode first. Parsec does what a browser does, from bytes:
// After: Parsecuse parsec_encoding::{Options, parse_bytes};
let page = b"<meta charset=windows-1251><p>\xcf\xf0\xe8\xe2\xe5\xf2</p>";let parsed = parse_bytes(page, Options::default()).unwrap();assert_eq!(parsed.encoding.name(), "windows-1251");assert_eq!(parsed.document.text(parsed.document.root()), "Привет");That’s parsec-web-encoding in Cargo.toml. Encoding has the options, including the HTTP charset.
None of the three has XPath. Parsec’s is in parsec-xpath:
// After: Parsecuse parsec_xpath::{Doc, Index, NoExtensions, Value, XPath};
let doc = parsec::parse("<ul><li>a<li class=x>b<li>c</ul>", false);let index = Index::build(&doc, &[doc.root()]);let q = XPath::new("count(//li[@class='x']/following-sibling::li)").unwrap();let n = q.evaluate(Doc::html(&doc, &index), doc.root(), &[], &[], &NoExtensions).unwrap();assert!(matches!(n, Value::Num(1.0)));Gotchas
Section titled “Gotchas”- Text is one string. scraper’s
el.text()is an iterator of the text nodes;doc.text(el)joins them for you, astextContentdoes. selectleaves out its scope.sel.select(&doc, li)searches underliand never returnsliitself. To test the node itself, usesel.matches(&doc, li, None).- Element names.
doc.lower_name(el)lowercases, as scraper’sname()gives.doc.kind(el)gives SVG’s case (foreignObject). - Removal is final; detaching isn’t.
doc.remove(id)is for good: its id reads as nothing after, and inserting it is anEditError. To move a node,doc.detach(id)and thenappendit somewhere else. <template>contents aren’t children. As in html5ever, they live in a separate fragment:doc.template_contents(id).
Checking your pages
Section titled “Checking your pages”The trees should match, since both follow the standard, so the check is that your extraction does. Run your old and new code over saved pages in a test and compare what comes out:
// Bothuse scraper::{Html, Selector};use parsec::css::SelectorList;
fn old(html: &str) -> Vec<String> { let doc = Html::parse_document(html); let sel = Selector::parse("a").unwrap(); doc.select(&sel).map(|a| a.text().collect()).collect()}
fn new(html: &str) -> Vec<String> { let doc = parsec::parse(html, false); let sel = SelectorList::parse("a").unwrap(); sel.select(&doc, doc.root()).into_iter().map(|a| doc.text(a)).collect()}
fn main() { let page = "<p><a href=/x>one</a> <b><a>t<i>wo</i></a></b>"; assert_eq!(old(page), new(page));}Then time both on those pages with a criterion or divan bench. The figures above are one machine and one set of pages; yours are the ones that matter.