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

Glossary

Beautiful Soup: parsing HTML in Python

Beautiful Soup is a Python library that turns HTML or XML, even badly broken markup, into a tree you can search and navigate with a few method calls.

What it is and is not

Beautiful Soup parses documents; it does not download them and does not run JavaScript. You fetch the page with an HTTP client such as Requests, hand the text to Beautiful Soup, and query the resulting tree. The current major version is published on PyPI as beautifulsoup4 and imported as bs4.

Choosing a parser

ParserSpeedNotes
html.parserModerateBuilt into Python, no extra install
lxmlFastNeeds the lxml package; also parses XML
html5libSlowParses exactly like a browser, best for very broken pages

Different parsers can build slightly different trees from the same broken HTML, so pick one and keep it.

Finding things

from bs4 import BeautifulSoup  # pip install beautifulsoup4 lxml

doc = BeautifulSoup(page_html, "lxml")
heading = doc.find("h1").get_text(strip=True)
rows = doc.select("table.specs tr")
specs = {r.th.get_text(strip=True): r.td.get_text(strip=True)
         for r in rows if r.th and r.td}
links = [a["href"] for a in doc.find_all("a", href=True)]

find and find_all search by tag name, attributes and text. select and select_one take CSS selectors, which many people find easier to read. Beautiful Soup does not support XPath; if you need it, parse with lxml directly.

Common pitfalls

  • None errors. find returns None when nothing matches, so chained calls crash. Check before you chain.
  • Content that is not there. If the element exists in your browser but not in the soup, the page builds it with JavaScript. Look for a JSON endpoint or render with a browser.
  • Encoding. Pass bytes (response.content) rather than text when the server declares the wrong charset, and let Beautiful Soup detect it.
  • Speed. For millions of pages, lxml with a narrow search is much faster than broad find_all calls on html.parser.

Navigating the tree

Besides searching, you can walk the tree: .parent, .find_next_sibling() and .children move between nodes. This helps with pages that label values only by position, such as a definition list where each label sits right before its value. Grab the label you know, then step to its sibling instead of guessing a class name.

Recognizing a challenge page

A frequent surprise is parsing a page that is not the page you asked for. A short document titled "Just a moment..." or containing a cf-turnstile div, a g-recaptcha div, or an AWS WAF challenge script means you received a check instead of content. Make your parser look for those markers first and return a clear signal rather than empty fields.

def challenge_kind(doc):
    if doc.select_one(".cf-turnstile[data-sitekey]"):
        return "turnstile", doc.select_one(".cf-turnstile")["data-sitekey"]
    if doc.select_one(".g-recaptcha[data-sitekey]"):
        return "recaptcha", doc.select_one(".g-recaptcha")["data-sitekey"]
    return None, None

With the kind and key in hand, a token task with GateSolver returns the answer (a Cloudflare Turnstile token typically under 3 seconds), and you post the form again with it. See Scrapy vs Beautiful Soup for when to move up to a framework.

Common questions

Does Beautiful Soup download web pages?

No. It only parses markup. Fetch the page with an HTTP client first and pass the content to it.

Which parser should I use with Beautiful Soup?

lxml for speed, html.parser when you want no extra install, html5lib for badly broken pages.

Can Beautiful Soup use XPath?

No. Use CSS selectors with select, or parse with lxml directly if you need XPath.

Why is an element I see in the browser missing?

The page probably adds it with JavaScript after loading, so the raw HTML never contained it.

How can I tell if I parsed a challenge page?

Look for markers such as a Turnstile or reCAPTCHA div with a data-sitekey, or a short title like Just a moment.

Keep reading