How to make a GET request in Python

1 min

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.