Web Scraping & Data Collection

HTML Parsing

Updated July 21, 2026

Extracting structured data from raw HTML by traversing the DOM tree and selecting elements via CSS selectors or XPath.

Also known as: DOM parsing, HTML DOM parsing, markup parsing, HTML tree parsing

HTML parsing is the process of turning raw HTML markup into a structured, traversable tree (the Document Object Model, or DOM) so that specific elements and data points can be located and pulled out programmatically. Once the markup is parsed, code queries the tree using CSS selectors or XPath to select the exact nodes it needs: a price inside a span, a heading, a table row, an attribute on a link. It is the extraction stage that sits between fetching a page and doing something with its contents, and it is foundational to almost every data-collection pipeline that reads the web.

The technique has two lineages that converged. The DOM is a cross-platform, language-independent tree representation of an HTML or XML document, standardized through the W3C, where each node represents part of the document: an element, an attribute, a text run. The formal parsing algorithm that produces that tree from messy real-world markup is codified in the WHATWG HTML Standard, which every modern browser implements so that even malformed HTML resolves to a consistent tree. Parsing HTML specifically for data extraction, rather than for rendering, grew alongside early web scraping in the late 1990s and 2000s; Python's BeautifulSoup, released in 2004, is widely credited with making ad-hoc parsing accessible to non-specialist developers, alongside later tools such as lxml, html5lib, jsdom, and Cheerio.

Today HTML parsing underpins search indexers, automated testing, price and content monitoring, and competitive-intelligence collection: any workflow that needs to read a page the way a machine can act on rather than a human can look at.

How HTML parsing works

Browser-grade HTML parsing runs in two stages, as specified by the WHATWG HTML Standard. First, tokenization turns the raw character stream into a sequence of tokens: doctype, start tags, end tags, comments, character data, and end-of-file. Second, tree construction feeds those tokens into a finite-state machine whose 'insertion modes' (states such as 'in head', 'in body', and 'in table') decide where each node attaches, building the DOM. This machinery exists largely to handle imperfect markup: unclosed tags, misnested elements, and omitted optional tags all resolve to a predictable tree.

Once the tree exists, extraction is a query problem. Code walks the DOM and selects nodes using CSS selectors or XPath, then reads their text, attributes, or child structure. Outside a full browser, libraries such as BeautifulSoup, lxml, and html5lib in Python, or Cheerio and jsdom in JavaScript, expose the same read-and-query model against an HTTP response, and the browser-native DOMParser Web API does it inside the page.

CSS selectors vs. XPath

CSS selectors and XPath are the two dominant ways to target elements in a parsed DOM, and choosing between them is the everyday decision in extraction work. CSS selectors use the same syntax front-end developers already know (id, class, attribute, and positional matches) and are generally simpler to write and quick for basic targeting. They are the natural choice when the data lives in an element you can name by class or structure.

XPath is more expressive. It can select by text content, traverse upward to parents and ancestors, express sibling and conditional logic, and it works natively against generic XML as well as HTML. That power matters when the element you want has no stable class and must be located relative to a label, or when you need to walk back up the tree from a matched node. In practice many pipelines mix both: CSS for the common cases, XPath where structure or text matching demands it. Neither changes what parsing produces; they are just two query languages over the same tree.

HTML parsing vs. web scraping and crawling

The three terms are often used interchangeably but describe different stages. Web scraping is the full end-to-end pipeline: fetch a page, render it if necessary, parse the markup, extract the fields, then store or act on them. HTML parsing is only the parse-and-extract step within that pipeline. Web crawling is different again: it is the discovery and traversal problem of following links to find which pages exist, not what to pull from any single one. A crawler decides which URLs to visit; a parser decides what to read once a URL's content is in hand.

A related distinction is parsing versus DOM manipulation. Parsing produces and reads the tree; manipulation, as in a live browser via JavaScript, mutates it. Scraping libraries almost always read rather than modify, since the goal is extraction, not changing a rendered page.

Where HTML parsing meets competitive monitoring

Static HTML parsers only see what is in the raw HTTP response. Many modern sites render key content client-side with JavaScript, so a plain parser finds an empty shell where the price, feature list, or job posting should be. The common fix is to run the page in a headless browser (Chrome driven by Playwright or Puppeteer) so scripts execute and inject content first; the resulting DOM is then parsed with the same selector-based approach.

For change-detection and competitive-monitoring tools, a further discipline applies. Rather than diffing raw HTML, best practice is to parse and track specific fields (a price via a CSS selector, a plan name, a headline) because that isolates meaningful change from markup and class-name churn that alters the HTML without changing anything a person would see. Teams monitoring competitor pricing pages, product pages, and job listings rely on parsed, targeted selectors to keep signal high and false positives low.

Stop looking terms up. Start tracking them.

meertrack watches your competitors' websites, pricing, and hiring, then alerts you when something meaningful changes.

Or compare 11 CI tools side by side →

Frequently Asked Questions

What is HTML parsing?

HTML parsing is the process of reading raw HTML markup and building it into a structured tree called the DOM, where each node represents part of the document. Once that tree exists, code can traverse it and select specific elements (using CSS selectors or XPath) to extract structured data such as prices, headings, links, or table contents. It is the extraction step that sits between fetching a page and using its content.

What is the difference between HTML parsing and web scraping?

Web scraping is the whole pipeline: request a page, render it if needed, parse it, extract the data, and store or act on it. HTML parsing is one stage inside that pipeline: the part that turns markup into a DOM tree and pulls out the fields you want. Put simply, all scrapers parse HTML, but parsing on its own does not fetch pages or manage the surrounding workflow.

What is the difference between CSS selectors and XPath?

Both query a parsed DOM to locate elements. CSS selectors use familiar front-end syntax (id, class, attribute, position) and are simpler and quick to write for straightforward targeting. XPath is more expressive: it can match by text content, traverse to parent and ancestor nodes, and express complex conditional logic, and it works on generic XML too. Many extraction pipelines use CSS for common cases and reach for XPath when structure or text matching requires it.

Why does HTML parsing fail on JavaScript-rendered pages?

A plain HTML parser only sees the raw HTTP response. When a site builds its content client-side with JavaScript, that content is not present in the initial markup, so selectors find nothing or an empty shell. The usual fix is to load the page in a headless browser such as Chrome driven by Playwright or Puppeteer, let the scripts run and inject content, then parse the resulting DOM with the same selector-based approach.

What is a DOM tree in HTML parsing?

The DOM, short for Document Object Model, is the tree that HTML parsing builds out of markup. Every node in it maps to a piece of the page: a tag, one of its attributes, or a run of text, all linked together in the parent-child ordering the source implies. Being a tree is what lets code walk through the structure and pinpoint nodes accurately, and that traversal is precisely what CSS-selector and XPath extraction depend on to work.

Related terms

← Browse the full glossary

You run the business.

We'll watch the competition.

14 days free. 3 competitors. Cancel anytime.