HTML is a markup language for structuring web documents with nested elements, tags, text, attributes, and classes. Its structure can be represented as objects whose string conversion produces the complete document. section 15.14 section 22.4
Common HTML tasks include:
- Representing an element hierarchy, such as html containing body and body containing p. section 15.14
- Finding elements by tag, such as links represented by the a tag. section 22.4
- Reading element text and attributes, such as a link’s text and href value. section 22.4
- Selecting elements by class, such as elements with the special class. section 22.4
Parsing a page
Load HTML into Beautiful Soup, then query its structure: bs = bs4.BeautifulSoup(html, "html.parser"); a_list = bs("a"); a_item = a_list[0]; a_item.text returns 'link', while a_item["href"] returns the URL. bs.select(".special") finds elements carrying the special class. section 22.4
para = p(text="this is some body text") doc_body = body(text="This is the body", subelement=para) doc = html(subelement=doc_body) print(doc) <html> <body> This is the body <p> this is some body text </p> </body> </html>
Shows a minimal nested HTML document and the generated string output, including html, body, and p elements. section 15.14











