Prerequisite: The
requests
Package
This exercise asks students to use Python to process static data files located on the Internet. It focuses on "GET" requests, which are the most common and perhaps the simplest kind of HTTP request.
This exercise should help students familiarize themselves with JSON-formatted data and enhance their familiarity with CSV-formatted data.
After completing this exercise, students should be ready to issue requests to real-life web services (APIs).
Students should focus on the JSON challenges below. Any student desiring further exploration may optionally also complete the CSV challenges.
HINT: the response text for each of the following challenges will be formatted as JSON, so you can use the
json
module to parse it. Also, depending on which URL we are making a request to, sometimes the JSON response will resemble a list and sometimes it will resemble a dictionary, so make sure to parse each accordingly.
Write a Python program which issues a GET request for this product.json data, then print the product's "name".
FYI: the response text will be a JSON object, like a Python dictionary.
Write a Python program which issues a GET request for this products.json data, then loop through each product and print its "name" and "id".
FYI: the response text will be a JSON array of objects, like a Python list of dictionaries.
Write a Python program which issues a GET request for this gradebook.json data, then calculate and print the average, min, and max grades.
HINT: the response text for each of the following challenges will be formatted as a CSV string, so you can you can use the familiar CSV-processing mechanisms like the the
csv
module or thepandas
package, with some possible modifications for parsing a CSV-formatted string instead of a CSV file.
Write a Python script which issues a GET request for this products.csv data, then loop through each product and print its "name" and "id".
Write a Python program which issues a GET request for this gradebook.csv data, then calculate and print the average, min, and max grades.