No history yet

Requests Installation and GET

Getting Data from the Web

Most modern applications need to interact with the internet, whether it's to grab data from a public service, talk to another part of a larger system, or just check if a website is online. While Python's standard library has tools for this, they can be complicated to use for everyday tasks. This is where the requests library comes in.

requests is the most popular, unofficial Python library for making HTTP requests. It wraps all the complex details of network communication in a simple, elegant interface. The library's philosophy is "HTTP for Humans," and it lives up to the promise. With just a couple of lines of code, you can fetch data from any URL on the web.

Installation

Because requests isn't part of Python's standard library, you need to install it first. You can do this using , Python's package installer. If you have Python installed, you likely have pip ready to go.

Open your terminal or command prompt and run the following command:

pip install requests

This command tells pip to find the requests package in the Python Package Index and download it to your machine. Once it's done, you're ready to start making requests.

Making a GET Request

The most common type of HTTP request is a GET request. It's what your browser does every time you type a URL into the address bar. It's a request to get information from a specific resource. In the requests library, this is done with the requests.get() function.

Let's try fetching data from a simple, public . We'll use GitHub's base API endpoint, which provides some basic information about their available resources.

import requests

# Make a GET request to the GitHub API
response = requests.get('https://api.github.com')

When you call requests.get(), it sends a request to the URL you provided. The function then waits for the server to respond and, once it does, returns the entire response as a special Response object. We've stored this object in a variable called response.

Inspecting the Response

The Response object contains everything the server sent back, including the data, headers, and, most importantly, the status code. The tells you whether your request was successful.

You can access the status code through the .status_code attribute. A successful GET request will almost always return a status code of 200.

import requests

response = requests.get('https://api.github.com')

print(response.status_code)

Running this code will print 200.

This confirms our request worked. Now we can look at the actual data the server sent. The raw text content of the response is available in the .text attribute. This attribute contains the entire HTML of a webpage or, in the case of an API, usually structured data.

import requests

response = requests.get('https://api.github.com')

# Print the first 300 characters of the response text
print(response.text[:300])

Checking the status code before trying to use the data is a crucial first step in writing reliable code that interacts with the web. You can combine this into a simple check:

import requests

response = requests.get('https://api.github.com')

if response.status_code == 200:
    print('Success!')
    # You can process the data here
    # print(response.text)
elif response.status_code == 404:
    print('Not Found.')
else:
    print(f'An error occurred: {response.status_code}')

This simple pattern is the foundation for building robust applications that can gracefully handle network issues and server errors.

Ready to test your knowledge?

Quiz Questions 1/5

What is the primary purpose of the Python requests library?

Quiz Questions 2/5

Which line of code correctly sends a GET request to https://api.example.com/data and stores the server's response?

That's how simple it is to get started. You now know how to install the requests library, make a basic GET request, and check the response to see if it was successful. This is the first step toward automating tasks and gathering data from the vast resources of the internet.