──────────────────────────────────────

47. Work with Web Data · Extract Workshop Fields from HTML

Use HTMLParser to collect the workshop title and day from selected start tags.

Lesson files · lessons/web-data/parse-workshop-html

Before you start
- Turn key–value pairs into a dictionary and look up named values.
- Override one method of a class and use the values it receives.

By the end
- Extract the titles and days of workshop entries in page order.
- Check that missing fields are skipped and HTML character references display as text.

Where you can use it
Turn a supplied workshop page into a list of titles and days for the organiser to check.

HTML describes a page with elements and attributes. Attributes store details such as a day or label. Give HTMLParser a string with feed(). Python calls your handle_starttag() method once for each opening tag, with the tag name and attribute pairs. dict(attrs) lets you look up attributes by name. Use data-kind to select workshop entries on this practice page. The parser decodes character references such as &amp; in attribute values. Practise with a saved page you made or a fixture supplied with permission, and check that any future source permits automated use.

Predict
What would the example print for a time element with no datetime attribute?
- It would print nothing.
- It would print the words inside the element.

Worked example (example.py)

from html.parser import HTMLParser

class TimeReader(HTMLParser):
    def handle_starttag(self, tag, attrs):
        time = dict(attrs).get("datetime")
        if tag == "time" and time:
            print(time)

reader = TimeReader()
reader.feed('<p>Starts at <time datetime="10:30">half past ten</time></p>')
reader.close()


Example result
10:30

Read one line of HTML. For each li with data-kind set to workshop, print its data-title, a space, a slash, a space and its data-day. Keep the order of the page. Skip other elements and entries that have an empty or missing title or day. Print nothing if no entries qualify.

Starter program (starter.py)

from html.parser import HTMLParser

class WorkshopReader(HTMLParser):
    def handle_starttag(self, tag, attrs):
        if tag == "li":
            print("Found a list entry")

reader = WorkshopReader()
reader.feed(input())
reader.close()


Starter input
<ul><li data-kind="workshop" data-title="Paper &amp; Ink" data-day="Saturday">Paper &amp; Ink</li><li data-kind="tour" data-title="Garden Tour" data-day="Saturday">Garden Tour</li><li data-kind="workshop" data-title="Kite Building" data-day="Sunday">Kite Building</li><li data-kind="workshop" data-title="Badge Making" data-day="Saturday">Badge Making</li></ul>

Hint
Use dict(attrs).get(...) for each attribute you need, and check that both values exist before printing.

Case 1. Try the sample
Input
<ul><li data-kind="workshop" data-title="Paper &amp; Ink" data-day="Saturday">Paper &amp; Ink</li><li data-kind="tour" data-title="Garden Tour" data-day="Saturday">Garden Tour</li><li data-kind="workshop" data-title="Kite Building" data-day="Sunday">Kite Building</li><li data-kind="workshop" data-title="Badge Making" data-day="Saturday">Badge Making</li></ul>
Expected output
Paper & Ink / Saturday
Kite Building / Sunday
Badge Making / Saturday

Case 2. Missing day
Input
<ul><li data-kind="workshop" data-title="Sketching"></li></ul>
Expected output
(No output.)

Case 3. Attributes in a different order
Input
<ul><li data-day="Friday" data-title="Repair Café" data-kind="workshop">Repair Café</li></ul>
Expected output
Repair Café / Friday

Stretch
Add another element with the same attributes and explain why the parser skips it.

Reflect
Which parts of the page does the parser rely on staying the same?

Check your prediction
It would print nothing. The method reads the attribute and only prints when its value is present.
