Skip to content

Latest commit

 

History

History
47 lines (24 loc) · 3.02 KB

File metadata and controls

47 lines (24 loc) · 3.02 KB

Web Requests Exercise

Prerequisite: the requests package

Objectives

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).

Challenges

JSON 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.

JSON Challenge 1 - Product

Write a Python program which issues a GET request for this product.json data, then print the product's "name".

Hint: the response text will be a JSON object, like a Python dictionary.

JSON Challenge 2 - Products

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".

Hint: the response text will be a JSON array of objects, like a Python list of dictionaries.

JSON Challenge 3 - Gradebook

Write a Python program which issues a GET request for this gradebook.json data, then calculate and print the average, min, and max grades.


CSV Challenges

HINT: the response text for each of the following challenges will be formatted as a CSV string, so you can read it using the csv module. But we are parsing CSV-formatted stings instead of CSV-formatted files, so you will need an alterantive approach than the one we have used so far. If you get stuck, consult this partial CSV solution. 😃

CSV Challenge 1 - Products

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".

CSV Challenge 2 - Gradebook

Write a Python program which issues a GET request for this gradebook.csv data, then calculate and print the average, min, and max grades.