How to make a GET request in Python

1 min readAPIRequests
7-Day Challenge

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.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

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"}
This is what we should get from this call
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 script to make an HTTP GET request against a website

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)
The script to make an HTTP GET request against a website

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.

7-Day Challenge

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.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

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

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.