Learnmonkey Learnmonkey Logo
× Warning:

The tutorial you are reading is currently a work in-progress.

Python Requests

What are Requests?

In this case, requests refers to HTTP requests. This means requesting stuff from a server over the internet. There are many different types of requests. GET means getting something from the server. You do this whenever you are visiting a website. Then, there's POST, which is for updating something on the server. PUT, DELETE, and PATCH are other HTTP request types. However, GET and POST are the two most common ones.

The requests Module

Python has built-in modules for doing HTTP requests like urllib, but the requests module makes it much simpler. Because requests is not in the Python standard library, we need to install it using PIP:

pip install requests If PIP doesn't work, read our Troubleshooting PIP article.

To test if you installed requests correctly, try running the following Python script:

import requests

If it works, you can now start using the requests module!

Making GET Requests

GET requests simply request data from a server. The data could be HTML (for websites), JSON (APIs), or even XML. In fact, your browser sends a GET request every time you visit a website. To send a GET request, we type the following:

requests.get("URL")

The function returns a response object.

For example:


import requests
response = requests.get("https://learnmonkey.github.io")
    

Once we have our response object, we can get the status code:

print(requests.status_code)

Output:

200 Note: A 200 status code means OK, so everything went smoothly.

If we tried requesting a page that doesn't exist, we get a 404 status code:


page_that_does_not_exist = requests.get("https://learnmonkey.github.io/page/that/does/not/exist")
print(page_that_does_not_exist.status_code)
    

The code returns:

404

We can also get the HTML (or whatever data) that was returned:

print(requests.text)

The code returns all of the HTML of the page:


<!doctype html>
<html lang="en"><head>
    <title>Home | Learnmonkey</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
<!--     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css"> -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
    <link rel="stylesheet" href="/assets/css/style.css">
The actual output is quite large, so it's been limited to only the first few lines.