Fetching Content with aiohttp in Python

Mar 3, 2024 ยท 3 min read

The aiohttp library is a powerful tool for making asynchronous HTTP requests in Python. One common task is to fetch content from a URL and work with the response. This guide will demonstrate practical examples of using aiohttp to get content, along with some nuances and best practices.

Basic Content Fetching

Fetching content with aiohttp starts by creating a ClientSession. This session handles all our requests behind the scenes:

import aiohttp

async with aiohttp.ClientSession() as session:
    async with session.get(url) as response:
        data = await response.text() 

The session.get() method returns a ClientResponse object that contains the response details. We use the await response.text() method to get the response content as a string.

There are also methods like response.read(), response.json(), and response.content to get the raw bytes, parsed JSON, or unparsed bytes respectively.

Handling Errors and Exceptions

Any network or HTTP errors will raise an exception, which we need to handle properly:

    async with session.get(url) as response:
        data = await response.text()
except aiohttp.ClientConnectionError:
    print("Connection error occurred") 
except aiohttp.ClientResponseError:
    print("Invalid response received")

This makes sure our program doesn't crash if the request fails.

Setting Request Headers

We can pass custom HTTP headers by providing a dictionary to the headers parameter:

headers = {"User-Agent": "My Program"}

async with session.get(url, headers=headers) as response:

This sets the User-Agent header for all requests made within this context.

Posting Form Data

In addition to GET requests, we can use sessions for POST requests with form data:

data = {"key": "value"}
async with session.post(url, data=data) as response:

The data dictionary will be form-encoded automatically.

Streaming Response Content

For very large responses, we may want to stream the content instead of loading the entire text or bytes into memory at once:

with open("file.pdf", "wb") as fd:
    async for data in response.content.iter_chunked(1024):

This streams the content in 1KB chunks while saving it to a file. This avoids needing gigabytes of RAM for huge downloads.

Configuring Timeout

We can set a timeout to avoid hanging requests with the timeout parameter:

# Timeout after 5 seconds
timeout = aiohttp.ClientTimeout(total=5)
async with session.get(url, timeout=timeout) as response:

After 5 seconds the request will be cancelled and raise a TimeoutError.

Practical Tips

Here are some handy tips when working with aiohttp:

  • Always handle exceptions properly in an asynchronous environment
  • Take advantage of streaming for large responses
  • Set reasonable timeouts to avoid hanging
  • Close sessions properly or use async context managers
  • Familiarize yourself with common response attributes like status, headers, etc
  • Wrap Up

    The aiohttp library provides a rich toolset for all kinds of HTTP requests and responses. Mastering asynchronous workflows takes some practice, but can lead to very fast and scalable programs in Python. The examples here demonstrate practical techniques for fetching content using aiohttp while avoiding some common pitfalls.

    Asynchronous programming opens up new capabilities, but keeping it simple and handling errors gracefully is always important. With robust handling of results and exceptions, aiohttp is a pleasure to work with for web content scraping, APIs, webhooks, and more.

    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>
        <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" />


    Don't leave just yet!

    Enter your email below to claim your free API key: