Project · Parsing

Parsec: The Parsing Foundation for the Web, in Rust

An HTML parser that beats Lexbor on every real page in its benchmark and on every input built to break it, with selectors, XPath, CSS and URLs alongside. JavaScript and XML are next, one crate at a time.

370 MB/sOn 13 real pages, Lexbor 242
8 of 8Hostile inputs won against Lexbor
4,565WHATWG URL cases, all passing
SilverOpenSSF Best Practices badge

Overview

Parsec is the HTML parser from Netweir, my web scraper, pulled out into a project of its own so anyone can use it. Same code: a script keeps the two in step, and CI fails the moment they drift.

It parses HTML the way a browser does, then gives you what you came for: CSS selectors, XPath, an editable tree, and where every node came from. CSS, URLs and text encodings come with it. JavaScript and XML parsers are on the way, each in its own crate.

The problem

A scraper ends up spending most of its time parsing. Fix the network and the profiler points straight at the HTML parser. The fastest one I could find was Lexbor, and it’s good. It’s also C, and when a page is built to hurt it, it can take seconds over something that should take milliseconds.

Then there’s everything else on a real page. CSS you need to resolve, URLs you need to join, a Shift_JIS page whose <meta> tag lies about its encoding, and, more and more, the data you want sitting in a <script> tag instead of the HTML.

What I built

A parser in Rust that set out to beat Lexbor page for page, and did. On 13 real pages, 24 MB from Wikipedia, MDN, GitHub, the BBC and others, Parsec parses 370 MB a second. Lexbor manages 242 and html5ever 82. A mixed crawl of 1,000 pages, broken fragments and mislabelled encodings included, takes Parsec 1.02 seconds and Lexbor 1.94. And 80,000 unclosed <div>s take Parsec 3 ms. Lexbor needs 5.2 seconds.

The benchmark is in the repo. It builds Lexbor from a pinned release and runs all three parsers on the same input, on the same machine, and the numbers are public.

Speed came with the things people keep asking Lexbor for:

  • Source positions. Byte ranges, lines and columns on nodes and attributes, when you ask for them.
  • Parse errors by name, matching all 7,042 of html5lib’s tokenizer error cases.
  • Selectors Level 4 that actually match, :lang(), :dir() and the form states included, plus XPath 1.0 tested against libxml2’s expressions.
  • CSS through the cascade, with all 822 properties typed and computed values worked out.
  • The WHATWG URL Standard, all 4,565 web-platform-tests cases passing, 1.33 times as fast as Ada, the URL parser inside Node.js.
  • Declarative shadow DOM, all 7,590 web-platform-tests cases.
  • Limits on time, size and depth, so a hostile page stops instead of stalling you.

It has two dependencies and one unsafe block.

Use it from anything

cargo add parsec-html      # Rust
pip install parsec-html    # Python, free-threaded too
npm install parsec-html    # JavaScript, through WebAssembly

The Python package speaks selectolax’s API, so moving over is mostly an import. It parses those 13 pages in 66 ms to selectolax’s 106. For C there are packages on every GitHub release for Linux, macOS and Windows, on x86-64 and ARM.

Built to be trusted

Every release tag is signed. The crates rebuild byte for byte, the C libraries carry GitHub attestations, and PyPI and npm get trusted publishing with provenance. It’s fuzzed nightly, and it holds the OpenSSF Best Practices silver badge, one criterion short of gold.

When a nested <template> crash turned up after 0.1.0, it was fixed in the open and shipped in 0.2.0 with a published advisory. Parsec has two maintainers, and every change needs the other one’s review.

Where it is now

Version 0.2.0 is on crates.io, PyPI and npm. On the main branch, not yet released: a JavaScript lexer and script parser that already parse every should-parse script in test262, an XML parser that passes 1,915 cases from the W3C suite, and a native Node.js package. 0.3.0 will be the first release with the JavaScript parser.

After that comes the reason for the JavaScript parser: pulling the JSON out of a page’s scripts without running them. Netweir gets it first. The roadmap has the order, and I wrote up how Parsec got here.

Try it

pip install parsec-html

The code, the benchmarks and the design notes are all on GitHub, Apache-2.0. If you’ve got a page that breaks it, I want to see it.

// Let's talk

You have ideas.
I have IT solutions.

Fifteen minutes on Zoom is usually enough to tell whether I'm the right person for what you're building.

Denver, CO · Available for architecture, infrastructure, and DevOps engagements