ME204 2026 Icon

💻 Week 01, Day 03 - Lab

Getting weather data with Python requests

Author

Dr Jon Cardoso-Silva

Last updated

15 July 2026

🥅 Learning Objectives

By the end of this lab, you should be able to: i) Read API documentation and find the right endpoint for a task, ii) Fetch data from a REST API using Python’s requests library, iii) Save a JSON response to a file and reload it, iv) Navigate a nested JSON structure using type(), len(), and dictionary keys, v) Write data to a CSV file by hand using a loop, vi) Formulate a definition, apply it to real data, and justify your conclusion.

Wednesday, 15 July 2026 | Either 2:00-3.30pm or 3.30-5:00pm 📍 Check your timetable for the location of your class

This morning you saw how curl fetches data from the terminal and how requests does the same thing in Python. This afternoon you do it yourself: call a weather API, save the response, turn it into a CSV by hand, and start asking questions about what the data tells you.

Today is also the first lab where you work in a Jupyter notebook inside VS Code. The notebook has the code cells. This page has the instructions and the timing. Keep both open side by side.

🛣️ Lab Roadmap

Open the starter notebook ME204_W01D03_Lab.ipynb in VS Code on Nuvolos (notebooks/).

Part Activity type Focus Outcome
Part I On your own Fetch today’s forecast A working API call and your first JSON response
Part II On your own Fetch a year of rain and save the JSON A saved london_2025.json in data/weather/
Part III Teaching moment, then on your own From JSON to CSV by hand A london_2025.csv written line by line with a loop
Part IV Teaching moment Exploring London’s rainfall Counts, totals, and a short written summary
🏆 Challenge On your own Was 2024 different? A second year of data and a comparison

👉 NOTE: When you see 💡 TEACHING MOMENT, watch and listen: your class teacher is showing something for the whole room. When you see 🎯 Action points, it is your turn to work in the notebook.

If you are working on your own machine, download a copy here:

Part I: Fetch today’s forecast (15 min)

You are about to make your first API call in Python. The notebook has a cell that is ready to run: work through the Action points below in order.

🎯 Action points

  1. Run the first code cell in the notebook and check the status code. A 200 means the request worked.
  2. Store the response as JSON in a variable called data and run type(data).
  3. Run data.keys() to see what the dictionary contains.
  4. Follow the cells down to data["daily"] and explore what is inside it: check the type, the keys, and the length of one of the lists.
  5. Print today’s date and maximum temperature from the first entry in each list.

The coordinates for London come from the Open-Meteo location search. Try it yourself: go to open-meteo.com/en/docs and type “London” in the search box. Watch the latitude and longitude fill in.

Part II: Fetch a year of rain and save the JSON (20 min)

The forecast endpoint gives you the next few days. For historical data, the API has a separate endpoint, and you need to find it in the docs and plug it into the notebook.

🎯 Action points

  1. Go to open-meteo.com/en/docs and find the Historical Weather API page.
  2. Copy the API URL and paste it into the notebook cell where url_archive = None, replacing the None.
  3. Run the request cell. You should get a 200 back.
  4. Explore the response the same way you did in Part I: check the type, the keys, and the length.
  5. Look at the first and last few dates to confirm you have a full year.
🔎 Stuck on the URL?

Look for “Historical Weather” in the sidebar on the Open-Meteo docs. The URL is similar to the forecast one, but the hostname is different.

Save the raw JSON

Now you need somewhere to put the file.

  1. Open the terminal inside VS Code (Terminal > New Terminal) and create the folder data/weather/ inside /files/. You did this with mkdir in yesterday’s lecture.
  2. Back in the notebook, fill in file_path = None with the relative path from the notebook to the JSON file. The notebook shows a folder tree to help you work it out.
  3. Run the cell. It saves the JSON and prints the path.
  4. Reload the file into a new variable and confirm the length matches.

What does “relative path” mean here? The notebook runs from /files/notebooks/. The file should end up in /files/data/weather/. How do you get from one to the other?

Part III: From JSON to CSV by hand (25 min)

You have 365 days of weather data stored as lists inside a dictionary. In this part you pull those lists out, loop through them by position, and write each row to a CSV file one line at a time.

This is tedious on purpose. Tomorrow you will build the same CSV with pandas in two lines, and writing it by hand today is what makes that comparison clear.

Looping by position with range

Your class teacher will show why the CSV is written by hand today, then walk through Section 3.1: the given loop that prints the first 5 rows with range(5). Watch how i picks the same position from dates, rain, and precip. After that, the Action points are yours.

🎯 Action points

  1. Run the cell that extracts dates, rain, and precip from the dictionary.
  2. Section 3.1: run the given loop and read the output so you can see how i connects the three lists at the same position.
  3. Section 3.1b: change the loop to print the last 5 days instead. Try it yourself before asking for help.
  4. Section 3.2: count how many days in 2025 had any rain at all. Write a loop with a counter variable.
  5. Section 3.3: write all 365 rows to a CSV file using range(len(dates)). Write the header line first, then one data line per day inside the loop.
  6. Section 3.4: open the CSV you just wrote and print the first 6 lines to check it looks right.
🔧 Common mistakes in the CSV loop
  • No line breaks: each line needs \n at the end.

Part IV: Exploring London’s rainfall (25 min)

You have a full year of daily weather data. Time to ask what it tells you about when and how much it rains in London.

Define, discuss, then build together

First you write a definition of a “rainy day” and join a short discussion. Then your class teacher builds the monthly counts and the rain versus precipitation totals step by step from the solutions notebook, line by line. Follow along in your notebook as they go.

🎯 Action points

  1. Section 4.1: before any coding, answer the two questions in the markdown cell. What counts as a “rainy day,” and which variable will you use?
  2. Section 4.2: join the discussion. Be ready to share what you chose and why.

What definition did you choose? Why did you pick rain_sum or precipitation_sum? What would change if you used a different threshold?

  1. Sections 4.3–4.5: follow along while your class teacher builds the monthly rainy-day counts, the rain and precipitation totals, and a short summary. Keep the pattern in your notebook for after class if you want to re-run it yourself.

🏆 Challenge: Was 2024 different?

If you finish Part IV, change the date range in your API call to fetch 2024 data. Save the new JSON as london_2024.json and repeat your analysis. Compare the two years: was one rainier, were the same months the wettest, and did the gap between rain and precipitation change?

✨ Wrap-up

Your class teacher will ask for any lingering questions such that we can try to address them in the morning session.

📎 Appendix

Files

What you start with (in notebooks/ on Nuvolos, or download from this page):

  • ME204_W01D03_Lab.ipynb (starter notebook)

What you create during the lab:

File Location Part
london_2025.json data/weather/ II
london_2025.csv data/weather/ III

Data sources

  • Weather data: Open-Meteo API, free when the use is not commercial.
  • London coordinates (51.5085, -0.1257): Open-Meteo location search for “London”.