How to make a GET request in Python
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
One of the simplest way to make a GET request in Python is with the requests library.
First we need to install the requests library using the pip python package manager
You can write this in your terminal, it will automatically install the requests library.
pip3 install requests
Getting JSON data
Then we can run this script which will make an HTTP GET request against the website "https://reqbin.com/echo/get/json" that should return
{"success":"true"}
import requests # We import the library
# We define the url of the website we want to query
url = "https://reqbin.com/echo/get/json"
# We perform an HTTP GET request against the url we provided
response = requests.get(url)
# If everything succeeded we should be able to print the json as a dict object
print(response.json())
The JSON dict is now accessible from response.json()
Getting HTML data
You could also perform an HTTP GET call against a standard website to retrieve its HTML. Let's try with https://quotes.toscrape.com/
import requests # We import the library
# We define the url of the website we want to query
url = "https://quotes.toscrape.com/"
# We perform an HTTP GET request against the url we provided
response = requests.get(url)
# If everything succeeded we should be able to print html of the requested page
print(response.content)
You could try to parse the resulting HTML in order to extract data or save the page as is in an .html file.
Here you are! You can now query any website using requests.
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚