Prerequisite:
- Practice using Python to request static data files located on the Internet, focusing on "GET" requests.
- Increase exposure to JSON-formatted data, and practice processing it in Python.
- Leverage built-in Python modules and third-party Python packages to speed development and enhance capabilities.
- Prepare to issue requests to get data from web services (APIs).
Use the GitHub.com online interface to create a new remote project repository called something like "web-requests-exercise". When prompted by the GitHub.com online interface, let's get in the habit of adding a "README.md" file and a Python-flavored ".gitignore" file (and also optionally a "LICENSE") during the repo creation process. After this process is complete, you should be able to view the repo on GitHub.com at an address like https://github.com/YOUR_USERNAME/web-requests-exercise
.
After creating the remote repo, use GitHub Desktop software or the command-line to download or "clone" it onto your computer. Choose a familiar download location like the Desktop.
After cloning the repo, navigate there from the command-line:
cd ~/Desktop/web-requests-exercise
Use your text editor or the command-line to create a file in that repo called "get_data.py", and then place the following contents inside:
# get_data.py
print("REQUESTING SOME DATA FROM THE INTERNET...")
Make sure to save Python files like this whenever you're done editing them. After setting up a virtual environment, we will be ready to run this file.
Create and activate a new Anaconda virtual environment:
conda create -n requests-env python=3.8 # (first time only)
conda activate requests-env
From within the virtual environment, install the requests
package:
pip install requests
From within the virtual environment, demonstrate your ability to run the Python script from the command-line:
python get_data.py
If you see the provided message, you're ready to move on to project development. This would be a great time to make any desired modifications to your project's "README.md" file (like adding instructions for how to setup and run the app like you've just done), and then make your first commit, with a message like "Setup the repo".
Iteratively develop your Python script to tackle each of the following 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 prints 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" attributes.
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.