Back to Python
2025-12-115 min read

Requests Module (Python Programming)

Learn Requests Module (Python Programming) step by step with clear examples and exercises.

Title: Python Requests Module - A full guide for Web Scraping and API Calls

Why This Matters

In the realm of web development, Python's Requests module is an indispensable tool for making HTTP requests and handling responses. Whether you're constructing a web scraper or interacting with APIs, this powerful library simplifies the process, saving you time and effort. Mastering its usage can lead to real-world applications such as data collection, automation, and integration with third-party services.

Prerequisites

Before delving into the Requests module, ensure you have a solid understanding of Python syntax, HTTP protocol, and familiarity with working in a terminal or command prompt. A basic knowledge of web scraping and APIs will also be beneficial. It is recommended to have some experience with handling exceptions and error handling as well.

Understanding HTTP Methods

Familiarize yourself with the various HTTP methods such as GET, POST, PUT, DELETE, etc., as they play a crucial role in making requests using the Requests module.

Core Concept

The Requests module allows you to send various types of HTTP requests and handle the responses. To install it, simply run pip install requests in your terminal or command prompt.

import requests

Make a GET request to an API endpoint

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

Access the content of the response

content = response.json() # For JSON responses, use .text() for plain text

Check the status code of the response

status_code = response.status_code


### Sending POST requests with data

To send a POST request with data, you can pass it as a dictionary to the `requests.post()` function:

data = {'key1': 'value1', 'key2': 'value2'}

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


### Using headers and cookies

Headers can be passed as a dictionary to the `requests.get()` or `requests.post()` functions:

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}

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


Cookies can be set using the `session()` function and the `cookies` parameter:

s = requests.Session()

s.cookies.set('cookie_name', 'cookie_value')

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


### Timeout Settings

You can set a timeout for your request using the `timeout` parameter:

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

Worked Example

Let's create a simple web scraper that fetches the headlines from a news website:

import requests
from bs4 import BeautifulSoup

Make a GET request to the news website

response = requests.get('https://www.example-news.com')

Parse the HTML content using BeautifulSoup

soup = BeautifulSoup(response.content, 'html.parser')

Find all headline elements and extract their text

headlines = [h1.text for h1 in soup.find_all('h1')]

Print the headlines

for headline in headlines:

print(headline)


### Using BeautifulSoup to navigate the HTML structure

To navigate the HTML structure, you can use various methods provided by BeautifulSoup such as `find_all()`, `find()`, and `select()`. For example:

Find all articles with class 'article'

articles = soup.find_all('div', class_='article')

for article in articles:

print(article.text)

Common Mistakes

1. Forgetting to import requests

Ensure you have import requests at the beginning of your script.

2. Not handling exceptions

Always wrap your code that interacts with the network in a try-except block to handle potential errors:

try:

Your code here

except Exception as e:

print(f'An error occurred: {e}')


### 3. Ignoring the status code
Always check the status code of the response to ensure the request was successful:

if response.status_code != 200:

print('Error: Unsuccessful request')


### 4. Not handling HTML structure changes
Websites may change their HTML structure, which can cause your scraper to break. To mitigate this, use CSS selectors instead of relying on specific element names or IDs.

#### Handling Dynamic Content

For dynamic content, you might need to use JavaScript rendering libraries such as Selenium or PhantomJS to render the page before scraping its content.

### 5. Not respecting rate limits
If the API you're interacting with has a rate limit, make sure to implement appropriate throttling to avoid exceeding it and getting blocked.

Practice Questions

  1. Write a script that fetches the content of a webpage and saves it as a text file.
  2. Create an API client to interact with a third-party weather service, retrieve the current temperature in Fahrenheit for a given city, and print the result.
  3. Modify the news scraper example to fetch headlines from multiple pages (pagination).
  4. Write a script that logs into a website using session cookies and retrieves sensitive data.
  5. Create a web scraper that extracts product information from an e-commerce site, including name, price, and image URL.
  6. Implement a simple rate limiter for your API client to avoid exceeding the rate limit set by the service you're interacting with.
  7. Write a script that sends authenticated requests using the Requests module, passing your API key as a header.
  8. Modify the news scraper example to handle dynamic content by rendering the page using Selenium or PhantomJS before scraping its content.
  9. Implement a basic retry mechanism for handling transient errors such as timeouts and connection issues.
  10. Write a script that downloads a file from a URL provided as command-line argument, saving it to the current working directory with a unique filename.

FAQ

1. How do I send authenticated requests using Requests?

You can pass your API key or credentials as headers:

headers = {'Authorization': 'Bearer your-api-key'}
response = requests.get('https://api.example.com/data', headers=headers)

2. How do I handle a situation where the server times out or returns an error?

Wrap your code in a try-except block to catch exceptions, and handle them appropriately:

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

Your code here

except Timeout:

print('Error: Request timed out')

except Exception as e:

print(f'An error occurred: {e}')


### 3. How do I handle HTML structure changes in my web scraper?
Use CSS selectors instead of relying on specific element names or IDs, and test your scraper regularly to ensure it still works as intended.

#### Handling Dynamic Content

For dynamic content, you might need to use JavaScript rendering libraries such as Selenium or PhantomJS to render the page before scraping its content.

### 4. How can I improve the performance of my Requests-based web scraper?
Consider using a library such as `concurrent.futures` to send multiple requests concurrently, and implement rate limiting to avoid overloading the server.

### 5. Can I use Requests for file uploads or downloads?
Yes, you can use the `requests.post()` function with the `files` parameter to upload files, and the `requests.get()` function with the `stream=True` parameter to download files in chunks.

### 6. How do I verify SSL certificates when making requests?
You can disable SSL certificate verification by setting the `verify` parameter to `False`:

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


However, it is generally not recommended to disable certificate verification for production use. Instead, you can add the root CA certificate to your system's trust store or install a custom CA certificate bundle.
Requests Module (Python Programming) | Python | XQA Learn