THINK FIRST·CODE LATER

← All labs

Scrape a station table robustly

Problem

Read an HTML page from standard input. The data is in <table id="stations"> with a header row (Station, District, Bikes, Capacity); there may be other tables and malformed rows.

  1. Parse the table by its id; keep only body rows with as many cells as headers; strip whitespace.
  2. Convert Bikes and Capacity with pd.to_numeric(errors="coerce").
  3. Print scraped 4 stations, 1 without a bike count, then page updated: 2026-03-02 08:15 (text of <p class="updated"> without the Updated: prefix, or unknown), then below 20% full: … (stations with bikes/capacity < 0.2, alphabetical, or none, ignoring rows without numbers), and bikes available: 13 of 42 docks (rows with both numbers).

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

v4-2-data-1.html

<html><body>
<h1>CityPulse bike stations</h1>
<p class="updated">Updated: 2026-03-02 08:15</p>
<table id="stations" class="table x7f2">
<thead><tr><th>Station</th><th>District</th><th>Bikes</th><th>Capacity</th></tr></thead>
<tbody>
<tr><td>North Gate</td><td>Lucheng</td><td>3</td><td>20</td></tr>
<tr><td>Library</td><td>Lucheng</td><td>9</td><td>12</td></tr>
<tr><td>Riverside</td><td>Ouhai</td><td>n/a</td><td>15</td></tr>
<tr><td colspan="4">Maintenance notice: Airport station closed</td></tr>
<tr><td>Park</td><td>Ouhai</td><td> 1 </td><td>10</td></tr>
</tbody></table>
<table class="ads"><tr><td>Advertisement</td></tr></table>
</body></html>
What your program must print for v4-2-data-1.html
scraped 4 stations, 1 without a bike count
page updated: 2026-03-02 08:15
below 20% full: North Gate, Park
bikes available: 13 of 42 docks

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), in the folder that holds both files:

python my_lab.py < v4-2-data-1.html

Windows PowerShell (the default terminal of VS Code on Windows) does not understand <:

Get-Content v4-2-data-1.html | python my_lab.py

PyCharm: Run → Edit Configurations → tick Redirect input from and choose the file.

Jupyter: put this at the top of the cell that holds your program:

import sys
sys.stdin = open("v4-2-data-1.html", encoding="utf-8")

Write it here, in Jupyter or in your IDE, then paste it. Run it on the sample input yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.