Skip to content

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.

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 Document is Send and Sync, so many threads can read and query one page at once. scraper’s Html isn’t Send unless you turn on its atomic feature, and isn’t Sync even then. kuchikiki’s nodes are Rcs and stay on one thread.
  • One crate for the rest. Selectors, editing and serialization are in parsec-web; XPath 1.0 is in parsec-xpath; bytes in any encoding, sniffed as a browser sniffs them, are in parsec-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. append and insert_before refuse 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 RcDom was 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 own Document, 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 ElementRef reads naturally (el.value().attr("href"), el.text()), and kuchikiki’s NodeRef can be edited from anywhere you hold one. In Parsec every read and write goes through &Document or &mut Document.
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
  • Nodes are numbers. A NodeId is a u32 index into its Document. It’s Copy, it doesn’t borrow, and you can keep it across edits. You pass it back to the document to read: doc.text(id), not el.text().
  • Selectors take a scope. select(&doc, scope) searches under scope and never returns scope itself. Use doc.root() for the whole page.
  • Attributes are an iterator. doc.attrs(el) yields (namespace, name, value) in source order. There’s no attr("x") shortcut yet; find is 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::parse returns a Result with a message. Edits that would break the tree return EditError instead of panicking.
// Before: scraper
use 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: Parsec
use 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.

// Before: kuchikiki
use 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: Parsec
use 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");

If you use html5ever with markup5ever_rcdom, you walk Rc<Node>s and match on NodeData:

// Before: html5ever and markup5ever_rcdom
use 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: Parsec
use 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.

// Before: scraper
use 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: Parsec
use 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.

// Before: kuchikiki
use 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 &amp; c</li></ul>"#);
// After: Parsec
use 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 &amp; 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.

// After: Parsec
use 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.

html5ever takes text, so you find the encoding and decode first. Parsec does what a browser does, from bytes:

// After: Parsec
use 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: Parsec
use 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)));
  • Text is one string. scraper’s el.text() is an iterator of the text nodes; doc.text(el) joins them for you, as textContent does.
  • select leaves out its scope. sel.select(&doc, li) searches under li and never returns li itself. To test the node itself, use sel.matches(&doc, li, None).
  • Element names. doc.lower_name(el) lowercases, as scraper’s name() 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 an EditError. To move a node, doc.detach(id) and then append it somewhere else.
  • <template> contents aren’t children. As in html5ever, they live in a separate fragment: doc.template_contents(id).

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:

// Both
use 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.