liveBook · Manning

Beautiful Soup

As explained in The Quick Python Book, Fourth Edition

Beautiful Soup is a Python library for parsing HTML and XML documents and navigating their parsed trees.

Beautiful Soup parses retrieved HTML into a navigable parser object, avoiding brittle string searches and regular expressions when extracting web data. Fetch the page separately, commonly with requests, then pass its text to BeautifulSoup with an HTML parser. section 22.4

Use the parsed tree to extract tags, attributes, and selected elements:

  • Call the parser with a tag name, such as bs("a"), to obtain matching link elements. section 22.4
  • Index the result like a list, then read a tag's visible content through .text. section 22.4
  • Read attributes with item["href"] or item["class"], receiving the attribute value or class list. section 22.4
  • Use select(".special") to find elements matching a class selector; returned elements remain parser objects. section 22.4

Extract a link and a special element

Parse test.html with bs4.BeautifulSoup(html, "html.parser"), then evaluate a_list = bs("a"). The first item exposes a_item.text as 'link' and a_item["href"] as 'http://bitbucket.dev.null'. Evaluating bs.select(".special") finds the span whose text is 'this is special'. section 22.4

Listing 22.1 File test.html
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<title>Title</title>
</head>

<body>
<h1>Heading 1</h1>

This is plan text, and is boring
<span class="special">this is special</span>

Here is a <a href="http://bitbucket.dev.null">link</a>

<hr>
<address>Ann Address, Somewhere, AState 00000
</address>
</body> </html>

Inspect the small HTML fixture containing a heading, ordinary text, a special span, a link, and an address element. section 22.4

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains parser construction, tag searches, indexing, text, attributes, and class selection, but not advanced parser behavior.
Examples
Uses test.html, an anchor element, a special span, and short expressions for retrieving text and attributes.
What it assumes
Assumes basic Python and HTML knowledge while demonstrating page parsing from already retrieved HTML.
What you can do after
Apply Beautiful Soup to parse retrieved HTML and extract links, visible text, attributes, and class-selected elements.
How it connects
Connects HTML parsing with requests, regular expressions, CSS class selection, and web scraping.

Also explained in

Compare how 2 books explain Beautiful Soup →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.