1
Hacker News
Tirsvad edited this page 2026-10-28 07:40:36 +01:00

English · Dansk · Bahasa Melayu

Scraping Hacker News

Lecture: Scraping a live website. The code is hacker_news.py (parsing) and http_client.py (one requests.get with a timeout and a User-Agent). The commands read the course's static copy of the page; --source live reads the real site and --source fixture reads the saved copy without network.

Step 1: look at the page

The page lists stories with a title, a link and a score.

Step 1: the page

What we gained: live data is richer than a local file, but it can change, so we also keep a saved copy for tests.

Step 2: inspect the markup

The browser's developer tools show the tags: a row tr.athing, the title link .titleline > a and a span.score in the next row.

Step 2: markup

What we gained: we find the right tag by inspecting the page before we write any code.

Step 3: titles

find_all on the title links and get_text().

Step 3: titles

What we gained: a list comprehension turns many tags into a plain list of strings.

get("href") of the same anchors.

Step 4: links

What we gained: the same element gives both text and attribute values.

Step 5: upvotes

The text 38 points is split and converted with int().

Step 5: upvotes

What we gained: scraped text must be converted to numbers before it can be compared.

Step 6: the top story

max over the upvotes finds the most popular article.

Step 6: top story

What we gained: once the data is clean, ordinary Python answers questions about it.

What we gained

We can fetch a page with Requests, find the rows, read titles, links and scores, convert them to usable types and answer a question about the data.