"Web Requests" Exercise
Last updated
Was this helpful?
Last updated
Was this helpful?
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:
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:
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:
From within the virtual environment, install the requests
package:
From within the virtual environment, demonstrate your ability to run the Python script from the command-line:
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.
FYI: the response text will be a JSON object, like a Python dictionary
FYI: the response text will be a JSON array of objects, like a Python list of dictionaries
HINT: the response text for each of the following challenges will be formatted as JSON, so you can use 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 , then prints the product's "name".
Write a Python program which issues a GET request for this , then loop through each product and print its "name" and "id" attributes.
Write a Python program which issues a GET request for this , then calculate and print the average, min, and max grades.