Glossary
HTML parsing: from markup to a queryable tree
HTML parsing is the step that reads raw markup and builds a tree of elements, attributes and text that your code can search and navigate.
Definition
An HTML document is text with tags. A parser reads it from start to finish, recognizes start tags, end tags, attributes, comments and text, and builds a tree in which every element knows its parent and children. Browsers do this to render a page; scrapers do it to find data. The tree a browser builds is the DOM, and most scraping parsers produce something very close to it.
Tolerant parsing
Real pages are full of mistakes: unclosed paragraphs, misnested tags, stray end tags, attributes without quotes. Browsers never refuse such pages. The HTML standard defines exactly how to recover from each error, so every compliant parser builds the same tree from the same broken input. A list item without a closing tag is closed when the next one opens; a <table> with text directly inside it moves that text above the table.
This matters because your selectors should match what you see in the browser's developer tools. A parser that follows the standard, such as html5lib in Python, parse5 in Node or the parser inside a headless browser, gives that result. Faster parsers such as lxml use their own recovery rules, which usually agree but sometimes differ on badly broken markup.
Common parsers
| Language | Library | Notes |
|---|---|---|
| Python | Beautiful Soup | Friendly API on top of html.parser, lxml or html5lib |
| Python | lxml | Very fast, supports XPath |
| Python | selectolax | Fast, CSS selectors |
| Node.js | Cheerio | jQuery style API on parse5 or htmlparser2 |
| Go | golang.org/x/net/html, goquery | Standard tokenizer plus selector layer |
Library behavior and defaults change between versions, so check the current docs for the one you use. See Beautiful Soup and Cheerio for more.
A short example
import lxml.html
doc = lxml.html.fromstring(page_html)
rows = []
for card in doc.cssselect("li.result"):
title = card.cssselect("h3")[0].text_content().strip()
link = card.cssselect("a")[0].get("href")
rows.append((title, link))
text_content() collects text from every descendant, which is usually what you want for headings that contain nested spans.
Why not regular expressions
A regex can pull a value out of a tiny, fixed snippet, but HTML nests, attributes reorder, quotes change and whitespace moves. A pattern written for today's markup breaks quietly tomorrow. A parser plus a selector expresses what you mean ("the price inside the product card") and survives cosmetic changes.
Parsing and challenge pages
A parser will happily parse a bot challenge page too. Before you parse for data, check that you received the real page: for example, the absence of a cf-turnstile or g-recaptcha element, or of a challenge script. If one is present, the parse is still useful, because it gives you the site key from the widget's data-sitekey attribute, the value a reCAPTCHA v2 or Cloudflare Turnstile task needs. See site key.
Common questions
What does an HTML parser produce?
A tree of elements, attributes and text nodes that you can navigate and query with selectors.
Why does my parser show a different structure than the browser?
Parsers recover from broken markup in slightly different ways, or the browser ran scripts that changed the page after loading.
Which Python parser should I use?
Beautiful Soup is friendly for most work, lxml is faster and supports XPath, and html5lib matches browser recovery most closely.
Can I parse HTML with regular expressions?
Only for trivial fixed snippets. For real pages a parser is more reliable and easier to maintain.
How do I find a CAPTCHA site key with a parser?
Select the widget element, such as one with class g-recaptcha or cf-turnstile, and read its data-sitekey attribute.
Keep reading
- DOMThe DOM is the live element tree that scripts keep changing, so it often differs from the HTML the server sent.
- Beautiful SoupThe forgiving Python library for parsing HTML and XML into a tree you can search by tag, attribute or CSS selector.
- CSS selectorCSS selectors are the compact pattern language for picking elements by tag, class, attribute and position, and most scraping tools speak it.
- How do I parse HTML with Beautiful Soup?Feed the page text to BeautifulSoup with a parser, then search the tree with select, find and get_text to pull fields.