Scrapeless Wiki

What Is HTML Parsing? – Definition and How It Works

Informational P2 what is html parsing

Learn what an HTML parser does, how error recovery builds a tree from broken markup, and why regular expressions are the wrong tool.

HTML parsing turns markup text into a tree of nodes that can be queried. Every extraction technique depends on it, and the interesting part is what happens when the markup is invalid β€” which, on the real web, is most of the time.

1. What a Parser Produces

<div class="card"><h3>Widget<p>Great value

becomes a tree in which the heading is closed, the paragraph is a sibling rather than a child, and the div wraps both β€” because the specification defines exactly how to repair that input.

The parsed tree is therefore not a transcription of the source. It is the source after standardised repair.

2. Error Recovery Is Specified

HTML's parsing algorithm defines how to handle unclosed tags, misnested elements, stray end tags, and elements appearing where they are not permitted.

That is why every browser builds the same tree from the same broken input, and why a compliant parser matches what a browser would produce. It is also why the tree can differ structurally from what the source text appears to say.

3. Why Regular Expressions Fail

HTML is nested and context-dependent; regular expressions are neither.

A pattern that extracts an attribute from a known tag can work. Anything involving nesting, optional closing tags, attributes containing angle brackets, or comments containing markup breaks in ways that are difficult to predict and worse to maintain.

The practical rule: a single attribute from a known simple string, maybe. Anything structural, use a parser.

4. Querying the Tree

Two languages cover extraction:

  • CSS selectors β€” concise, universally supported, sufficient for class, id, attribute, and structural matching.
  • XPath β€” adds text matching and upward traversal, which CSS cannot express.

Most work is CSS with XPath reserved for text-anchored cases.

5. Parsers Differ in Tolerance and Speed

Parser Character
Browser-grade Follows the specification's recovery exactly; slowest
C-based Fast, tolerant enough for nearly all real pages
Standard-library No dependency, slower and less forgiving
Minimal Fastest, CSS selection only

The usual default is a friendly API over a fast C-based parser, escalating to browser-grade recovery only for pathologically broken pages.

6. Encoding Precedes Parsing

A parser needs correctly decoded characters. Passing bytes decoded with the wrong encoding produces a tree full of garbled text that parses without error.

Use the declared charset from the response header, falling back to the document's own declaration, and decode explicitly rather than relying on a platform default.

7. Parsing Is Not Rendering

A parser builds a tree from the markup it is given. It does not execute scripts, so anything a page adds after load is absent.

That distinction is the single most common source of confusion: the parser is working correctly, and the content was never in the document it was given.

8. Summary

HTML parsing builds a queryable tree using a specified error-recovery algorithm, so the tree is the markup after standardised repair rather than a literal transcription.

Use a parser rather than regular expressions for anything structural, decode encoding explicitly first, and remember that parsing does not execute scripts β€” missing content is usually a rendering question, not a parsing one.