1. Connecting Python to the Web with APIs
Welcome to Working with Web APIs & Data! An API (Application Programming Interface) allows your Python scripts to communicate directly with external web servers—fetching live weather data, financial rates, sports scores, or news feeds. In this lesson, you will learn how to use Python's community-standard requests package to fetch, parse, and handle structured JSON data safely over HTTP.
Lesson Objectives
- Understand fundamental web concepts: HTTP verbs (
GET,POST) and JSON data formats. - Send HTTP requests using the third-party
requestslibrary. - Parse incoming JSON HTTP response bodies into native Python dictionaries and lists.
- Evaluate HTTP response status codes and handle network failure gracefully.
2. HTTP Request & Status Code Matrix
When Python requests data from an API, the remote server returns a numerical Status Code indicating the outcome of the request:
| Status Code | Category | Meaning | Python Action |
|---|---|---|---|
200 OK |
Success | Request succeeded and data is returned | Safe to parse JSON response via response.json(). |
400 Bad Request |
Client Error | Invalid request parameter or missing query input | Check query parameters and request payload. |
401 / 403 |
Auth Error | Unauthorized / Invalid or missing API key | Verify authorization headers or query API keys. |
404 Not Found |
Client Error | Requested endpoint URL does not exist | Verify the base URL and API resource path. |
500 Internal Error |
Server Error | The remote API server crashed or is offline | Retry request after delay or notify user gracefully. |
3. Production Pattern: Fetching Live JSON Data
The code below demonstrates how to query a REST API, convert JSON output into a Python dictionary, and wrap execution in exception safety checks using raise_for_status():
import requests
def fetch_crypto_price(coin_id="bitcoin"):
url = f"https://api.coingecko.com/api/v3/simple/price"
params = {
"ids": coin_id,
"vs_currencies": "usd"
}
try:
# 1. Send GET request with a 5-second connection timeout
response = requests.get(url, params=params, timeout=5)
# 2. Check for HTTP errors (4xx / 5xx triggers exception)
response.raise_for_status()
# 3. Parse JSON response into a native Python dictionary
data = response.json()
# Guard clause: Verify coin key exists in output
if coin_id not in data:
print(f"Error: Coin '{coin_id}' not found.")
return None
price = data[coin_id]["usd"]
return price
except requests.exceptions.Timeout:
print("Error: The request timed out. Please try again later.")
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except requests.exceptions.RequestException as err:
print(f"Network error: {err}")
return None
# Usage example:
current_price = fetch_crypto_price("bitcoin")
if current_price:
print(f"Current Bitcoin Price: ${current_price:,.2f}")
4. Common Pitfalls Checklist
⚠️ API & Web Data Pitfalls:
- Forgetting
timeoutParameters: By default,requests.get()can hang indefinitely if a remote server stops responding! Always set a explicit timeout (e.g.,timeout=5). - Hardcoding Secret API Keys: Never upload scripts containing raw private API keys to GitHub. Use environment variables or configuration files.
- Assuming Every Response is Valid JSON: Server crashes often return HTML error pages instead of JSON, causing
response.json()to throw aJSONDecodeError. Always check status codes first!
5. Interactive Practice
Mini Practice: Parsing API Weather Payload
Write a utility function extract_temperature(api_data) that takes a nested dictionary response from an API (e.g., {"main": {"temp": 72.5}, "name": "London"}) and uses safe dict access to return a formatted string "London: 72.5°F". Return "Data unavailable" if keys are missing.
API Practice Complete!
Excellent work! You now know how to navigate nested JSON payloads and protect your scripts from missing fields.
6. Knowledge Check Quiz
Test Your Web API Knowledge
1. Which HTTP status code represents a successfully completed request that returned data?
2. Why should you always specify a `timeout` parameter in `requests.get()` calls?
7. Key Takeaways
- APIs allow Python programs to interact with external web data streams over standard HTTP requests.
- Use
requests.get()with parameters, timeouts, andresponse.raise_for_status()for safe fetching. - Convert JSON web data into Python dicts/lists using
response.json(). - Always wrap web network calls inside
try-exceptblocks catchingrequests.exceptions.RequestException.
🎓 Core Curriculum Complete!
Congratulations on finishing the core Python theory lessons! You are now ready to put your knowledge to work by building the **Major Capstone Projects**.