Advanced Python: Using APIs (Internet Data)
Goal
- Fetch data from the internet
- Understand JSON
Step 1: Understanding APIs
API = interface that provides you with data from the internet.
Examples:
- Weather data
- Bitcoin price
- News
Step 2: requests Module
import requests
response = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")
data = response.json()
print(data)
Explanation:
requests.get() → fetches data from the internet
.json() → converts the response into a Python dictionary
Step 3: Reading Data
print(data["bpi"]["USD"]["rate"])
Explanation:
- Accesses the Bitcoin price in USD
- JSON works like a nested dictionary
Task
- Fetch current data (e.g., weather or prices)
- Display only the important information