Fetching Data from APIs with Python Requests

Feb 3, 2024 ยท 2 min read

Accessing data from web APIs is a common task in Python programming. The Requests library makes it simple to send HTTP requests and get responses in Python. In this guide, I'll demonstrate how to use Requests to retrieve data from a sample API.

Making a GET Request

First, install Requests using pip:

pip install requests

Import Requests at the top of your script:

import requests

Make a GET request to the API endpoint and store the response:

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

This sends a GET request to the API and saves the response.

Processing the Response

The response contains the data from the API. To access the JSON data:

data = response.json()

You can now work with the data as regular dictionaries and lists in Python.

For example, to print the first item title:

print(data[0]['title']) 

Handling Errors

It's good practice to check the status code of the response:

if response.status_code == 200:
   # Success - carry on
   print(response.json())
else:
   # An error occurred
   print('Error:', response.text) 

This prints the JSON if the request succeeded or the error message if not.

Conclusion

The Requests library provides a simple way to interact with web APIs in Python. By sending GET requests and processing the response, you can retrieve and work with JSON data in your scripts.

The full power of Requests comes from additional features like custom headers, POST requests, and more. Check out the Requests documentation to learn about these in more depth.

Browse by tags:

Browse by language:

The easiest way to do Web Scraping

Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


Try ProxiesAPI for free

curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
...

X

Don't leave just yet!

Enter your email below to claim your free API key: