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

48. Work with Web Data · Build a Community Workshop Digest

Combine a JSON schedule with a saved HTML listing and show where the schedule came from.

Lesson files · lessons/web-data/community-workshop-digest

Before you start
- Use a saved JSON response when a fixture signals failure.
- Read selected attributes from HTML with HTMLParser.
- Filter records by a supplied value.

By the end
- Report the matching workshops for the day in the selected JSON response.
- Use the saved-copy and empty-day cases to explain the source label and the no-workshops message.

Where you can use it
Give a club organiser the workshop names for the selected day and say whether the day came from fresh data or a saved copy.

A club organiser needs a short list of workshops to announce for the next open day. A fictional JSON feed gives the day, and the saved page lists the workshops. Choose the JSON body before parsing it. Keep matches from the HTML in a list so your main program can decide how to present an empty day and how to label the source. Test a normal result, a fallback and a day without any workshops.

Predict
What does matches contain when day is changed to Monday?
- Both titles.
- An empty list.

Worked example (example.py)

events = [("Thursday", "Book swap"), ("Friday", "Badge making")]
day = "Friday"
matches = [title for event_day, title in events if event_day == day]
for title in matches:
    print(f"- {title}")


Example result
- Badge making

Build a weekly digest for the community club. Read four lines in order. The first is 200, 503 or timeout. Next come the fresh JSON body, the saved JSON body and a line of HTML workshop entries. Each available JSON body has a day field containing a weekday name. Only use the fresh body on 200. The second line is a dash in failure scenarios. Print Workshop digest for followed by the chosen day, then Source: fresh or Source: saved copy. From the HTML, list the workshops from that day in page order, beginning each with a dash and a space. A workshop entry is an li with data-kind="workshop" and nonempty data-title and data-day. If the list is empty, print No workshops listed.

Starter program (starter.py)

outcome = input()
fresh_body = input()
saved_body = input()
html_page = input()

# Choose and decode the schedule, then collect matching workshops.
print(f"Digest scenario: {outcome}")


Starter input
200
{"day":"Saturday"}
{"day":"Sunday"}
<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
Reuse your workshop reader and keep titles in a list on the reader. Pass it the selected day before calling feed().

Case 1. Try the sample
Input
200
{"day":"Saturday"}
{"day":"Sunday"}
<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
Workshop digest for Saturday
Source: fresh
- Paper & Ink
- Badge Making

Case 2. Saved schedule after a timeout
Input
timeout
-
{"day":"Sunday"}
<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
Workshop digest for Sunday
Source: saved copy
- Kite Building

Case 3. Service unavailable and no matches
Input
503
-
{"day":"Monday"}
<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
Workshop digest for Monday
Source: saved copy
No workshops listed.

Project checklist
Run the sample case.

Project checklist
Try one different case and explain what happened.

Marking criterion
Program behaviour (4 points)
Each required behaviour works on the supplied cases.

Marking criterion
Use of the lesson concept (3 points)
The code uses the main idea in a clear, purposeful way.

Marking criterion
Test evidence (2 points)
The reflection describes a case, its expected result and what happened.

Marking criterion
Explanation (1 points)
The learner explains a change or design decision in their own words.

Stretch
On a local computer, save your source as main.py. In the same folder, save the four sample input lines as a UTF-8 text file named workshop-input.txt. Run python3 main.py < workshop-input.txt in a macOS or Linux terminal, or py main.py < workshop-input.txt in Windows Command Prompt. The < gives the file’s lines to input() in order. Edit the saved fixtures to try your own fictional workshop, and run the same command again.

The ZIP includes added practice files for this lesson. Open its lesson folder and read the included instructions.

Reflect
What would you change if the event organiser started using a different field name on the page?

Check your prediction
An empty list. The comparison keeps only titles for the selected day.
