0Pricing
Learn AI with Python · Lesson

Working with Public Data APIs

OpenWeatherMap, Wikipedia, public government APIs — practical data collection examples.

Working with Public Data APIs is a free Learn AI with Python lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Practicing with Public APIs

The best way to learn data collection is to hit real APIs. This lesson uses three friendly ones:

  • JSONPlaceholder — a fake REST API for practice, no key needed
  • OpenWeatherMap — real weather data, free API key
  • Wikipedia API — article content and search

JSONPlaceholder for Practice

JSONPlaceholder serves fake posts, users, and comments with no authentication. It is perfect for testing your request and parsing code.

import requests

resp = requests.get("https://jsonplaceholder.typicode.com/posts", timeout=10)
resp.raise_for_status()
posts = resp.json()
print(len(posts), "posts")
print(posts[0]["title"])

Response to DataFrame Workflow

The standard workflow: fetch JSON, then load it straight into a DataFrame for analysis.

import pandas as pd

posts = requests.get("https://jsonplaceholder.typicode.com/posts", timeout=10).json()
df = pd.DataFrame(posts)
print(df.head())
print(df["userId"].value_counts())

OpenWeatherMap — Getting a Key

OpenWeatherMap requires a free API key passed as the appid parameter. Keep keys out of code — read them from an environment variable.

import os

API_KEY = os.environ["OWM_API_KEY"]   # set OWM_API_KEY in your shell

OpenWeatherMap — Query Parameters

The current-weather endpoint takes q (city), appid (key), and units (metric/imperial).

import requests, os

params = {
    "q": "Istanbul",
    "appid": os.environ["OWM_API_KEY"],
    "units": "metric",
}
resp = requests.get("https://api.openweathermap.org/data/2.5/weather", params=params, timeout=10)
resp.raise_for_status()
data = resp.json()

Extracting Nested JSON Fields

Real API responses are deeply nested. Drill in with chained keys, and use .get() with defaults for fields that may be missing.

temp = data["main"]["temp"]
humidity = data["main"]["humidity"]
desc = data["weather"][0]["description"]
print(f"{temp} C, {humidity}% humidity, {desc}")

Collecting Weather for Many Cities

Loop over cities, flatten the fields you care about into a list of dicts, then build a DataFrame.

cities = ["Istanbul", "London", "Tokyo"]
rows = []
for c in cities:
    p = {"q": c, "appid": os.environ["OWM_API_KEY"], "units": "metric"}
    d = requests.get("https://api.openweathermap.org/data/2.5/weather", params=p, timeout=10).json()
    rows.append({"city": c, "temp": d["main"]["temp"], "desc": d["weather"][0]["description"]})
import pandas as pd
df = pd.DataFrame(rows)

The Wikipedia API

The Wikipedia API exposes search and article content. It expects an action and format=json, plus a descriptive User-Agent header.

params = {
    "action": "query",
    "list": "search",
    "srsearch": "machine learning",
    "format": "json",
}
headers = {"User-Agent": "data-collector/1.0 (you@example.com)"}
resp = requests.get("https://en.wikipedia.org/w/api.php", params=params, headers=headers, timeout=10)
results = resp.json()["query"]["search"]

Fetching Article Extracts

Use prop=extracts with exintro=True to fetch the intro paragraph of an article — handy for building text datasets.

params = {
    "action": "query",
    "prop": "extracts",
    "exintro": True,
    "explaintext": True,
    "titles": "Artificial intelligence",
    "format": "json",
}
resp = requests.get("https://en.wikipedia.org/w/api.php", params=params, timeout=10)
pages = resp.json()["query"]["pages"]
for pid, page in pages.items():
    print(page["extract"][:300])

Reading API Documentation

Every API differs. Before coding, read the docs for: the base URL, required parameters, authentication method, response shape, and rate limits.

Test one request interactively, inspect resp.json(), then build your loop around the real structure.

Being a Good API Citizen

Public APIs are shared resources. Respect them:

  • Send an identifying User-Agent
  • Cache results so you do not refetch the same data
  • Sleep between requests and honor rate limits
  • Read and follow each API terms of use

Quick Check: Practice API

You want to test your request-and-parse code without signing up for an API key.

Recap: Public Data APIs

You practiced collecting from real APIs:

  • JSONPlaceholder for no-auth practice
  • OpenWeatherMap with q, appid, units params and a key from the environment
  • Wikipedia API for search and article extracts
  • The fetch to DataFrame workflow and how to drill into nested JSON
  • Good-citizen habits: User-Agent, caching, rate limits, terms of use

That completes data collection. Next: regular expressions for text AI.

Frequently asked questions

Is the “Working with Public Data APIs” lesson free?

Yes — the full text of “Working with Public Data APIs” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Working with Public Data APIs”?

OpenWeatherMap, Wikipedia, public government APIs — practical data collection examples. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Working with Public Data APIs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. REST API Fundamentals for Data Collection
  2. Paginating and Collecting Large Datasets
  3. Storing Collected Data Efficiently
  4. Working with Public Data APIs
← Back to Learn AI with Python