Book 5 — Web Scraping with Python

Python for All

Chapter Seven — From Dictionaries to DataFrames

Thanasis Troboukis  ·  All Books

Book Five · Chapter Seven

From Dictionaries to DataFrames

A scraper does not stop at finding tags. It turns each article into a dictionary, collects those dictionaries in a list, loads the list into pandas, and saves the result to CSV. This chapter builds that full single-page workflow.

One Card Becomes One Dictionary

The natural unit of scraped data is a dictionary. One article card becomes one dictionary with named fields like title, link, author, and published_at.

Python · Try it

      
This uses basic Python you already know: strings, variables, dictionaries, and function calls. Scraping is not a new language. It is ordinary Python applied to HTML.

Many Cards Become a List of Dicts

The next step is a loop: find all article cards, create one dictionary per card, and append() each dictionary to a list.

Python · Try it

      

This is the core data-collection loop of the whole book: find many containers, loop over them, build dictionaries, append them to a list.

Books 1, 2, and 3 all appear here: variables, list creation, for loops, if guards, dictionaries, and append(). The scraper works because these basic tools combine well.

List of Dicts to DataFrame

Pandas reads a list of dictionaries directly. Each dictionary becomes one row. Each key becomes one column.

Python · Try it

      
Why DataFrames matter: once the data is in pandas, filtering, sorting, exporting, and later analysis become much easier.

Saving the First CSV

A DataFrame can be written to disk with to_csv(). This is the point where scraping becomes useful outside the notebook.

Python · Copy to your notebook

The CSV file will contain one row per article and two columns: title and link.

Make links absolute: many sites use relative links like /society/.... Add the base URL before saving so the CSV contains complete links that work everywhere.

Your Turn — Scrape One Page

The snapshot below contains four article cards. Extract the titles and links, build a DataFrame, and preview the CSV text.

Python · Your turn

      
What you learned in this chapter: how one article becomes one dictionary, how many dictionaries become a list, how pandas turns that list into a DataFrame, and how to save a one-page scraper to CSV. In the final chapter you will repeat the same idea across page 1, page 2, page 3, and beyond.

Chapter Navigation

Move between chapters.

Loading Python environment — this may take a moment…