NEWStandard createTask and in.php APIs. Change the address and you are connected.View API docs
GATESOLVER

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

LanguageLibraryNotes
PythonBeautiful SoupFriendly API on top of html.parser, lxml or html5lib
PythonlxmlVery fast, supports XPath
PythonselectolaxFast, CSS selectors
Node.jsCheeriojQuery style API on parse5 or htmlparser2
Gogolang.org/x/net/html, goqueryStandard 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