Lecture 3
Fetching and integrating data from web APIs for machine learning and data analysis projects
This work is licensed under CC BY-NC-SA 4.0
© Way-Up 2025
https://api.example.com/usersGET: Retrieve data.POST: Create new data.PUT: Update existing data.DELETE: Remove data.GET for data retrieval.We use the requests library in Python.
import requests
# Define the API endpoint URL
url = "https://jsonplaceholder.typicode.com/posts/1"
# Send a GET request
response = requests.get(url)
# The response object contains server's response
print(response.status_code) # e.g., 200
print(response.json()) # The data in JSON format
https://jsonplaceholder.typicode.com/usersrequests.GET request to the endpoint./users/1 to get user with ID 1./posts?userId=1Passing query parameters with requests:
params = {'userId': 1}
response = requests.get('.../posts', params=params)
print(response.url) # See the final URL
https://jsonplaceholder.typicode.com/commentspostId=1.
headers = {'Authorization': 'Bearer YOUR_API_KEY'}
response = requests.get(url, headers=headers)
/posts/99999).status_code to see if it's 200.try...except block to catch network errors.Orange.data.Table assigned to out_data.
from Orange.data import Table, Domain, ContinuousVariable, StringVariable
import requests
# 1. Fetch data from API
users_data = requests.get('.../users').json()
# 2. Define the domain (the columns)
domain = Domain([
StringVariable('name'),
StringVariable('email'),
StringVariable('city')
])
# 3. Create the data table
data = [[u['name'], u['email'], u['address']['city']] for u in users_data]
out_data = Table.from_list(domain, data)
https://restcountries.com/v3.1/allout_data.