Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 7.12 — XML and HTML: Reading Markup
Python · Example 1 of 4

Read, search, build and write XML

Add a third book to the text and see every list grow. Then ask for a tag that does not exist, to see None.

XML and HTML describe data as a tree of nested elements with attributes and text. Python's xml.etree.ElementTree reads and writes XML, html.parser and BeautifulSoup read messy HTML, and any XML that comes from outside needs a hardened parser such as defusedxml.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 7 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Parse <cart><item name='pen' qty='2'/><item name='ink' qty='5'/></cart> with ET.fromstring and print the total of all qty values (it should print 7). Hint: attributes are strings, so use int().

02

Write a function quantities(text) that takes the same kind of cart text and returns a dictionary that maps each item name to its quantity as an int. An item without a qty attribute counts as 1. Test it with <cart><item name='pen' qty='2'/><item name='ink'/></cart>. Hint: get accepts a default value.

03

Use html.parser to collect the text of every <li> in <ul><li>a</li><li>b</li></ul> into a list and print it. Hint: make a subclass of HTMLParser with a flag that is on between <li> and </li>.

04

Use BeautifulSoup to print every href and its link text from <p><a href='/x'>X</a> and <a href='/y'>Y</a></p>, one pair per line. Hint: find_all("a"), then a["href"] and a.get_text().

Explain it without notes

01

What are the parts of an XML element, and how do you read each in ElementTree?

02

What is the difference between find, findall, findtext and iter, and what do they return when nothing matches?

03

Why can a search on a namespaced document return nothing, and how do you fix it?

04

Why should you not parse HTML with ElementTree or a regular expression, and what should you use instead?

05

What are the risks of parsing untrusted XML and how do you defend against them?

Read, search, build and write XML
Sign in to run this example in your browser.

Expected output

root: library {'name': 'City Library'}
children: ['book', 'book', 'magazine']
b1 2019 Learning Python 450.5 INR
b2 2022 Data at Work 620.0 INR
all titles: ['Learning Python', 'Data at Work', 'Weekly Code']
path search: Asha Rao
by attribute: Data at Work
missing: None
missing with default: n/a
<catalog version="1">
  <item name="pen">10</item>
  <item name="ink &amp; paper">3</item>
</catalog>
<?xml version='1.0' encoding='utf-8'?>
<catalog version="1">
  <item name="pen">10</item>
  <item name="ink &amp; paper">3</item>
</catalog>
[('pen', 10), ('ink & paper', 3)]
ParseError: mismatched tag: line 1, column 8