How to use cURL in Python?

cURL is a popular HTTP client tool and a C library (libcurl). It can also be used in Python through many wrapper libraries.

The most popular library that uses libcurl in Python is pycurl. Here's an example use:

import pycurl
from io import BytesIO

# Set the URL you want to fetch
url = 'https://www.example.com/'

# Create a new Curl object
curl = pycurl.Curl()

# Set the URL and other options
curl.setopt(pycurl.URL, url)
# Follow redirects
curl.setopt(pycurl.FOLLOWLOCATION, 1)
# Set the user agent
curl.setopt(pycurl.USERAGENT, 'Mozilla/5.0')

# Create a buffer to store the response and add it as result target
buffer = BytesIO()
curl.setopt(pycurl.WRITEFUNCTION, buffer.write)

# Perform the request
curl.perform()

# Get the response code and content
response_code = curl.getinfo(pycurl.RESPONSE_CODE)
response_content = buffer.getvalue().decode('UTF-8')

# Print the response
print(f'Response code: {response_code}')
print(f'Response content: {response_content}')

# Clean up
curl.close()
buffer.close()

Compared to other libraries like requests and httpx pycurl is very low level can be difficult to use however it has access to many advanced features like HTTP3 support that other libraries don't have.

pyCurl doesn't support asynchronous requests which means it can't be used in asynchronous web scraping though can still be used using threads. See mixing sync code using asyncio.to_thread() for more details

Question tagged: Python, HTTP

Related Posts

How to Scrape StockX e-commerce Data with Python

In this first entry in our fashion data web scraping series we'll be taking a look at StockX.com - a marketplace that treats apparel as stocks and how to scrape it all.

Web Scraping Simplified - Scraping Microformats

In this short intro we'll be taking a look at web microformats. What are microformats and how can we take advantage in web scraping? We'll do a quick overview and some examples in Python using extrcut library.

How to Scrape Twitter with Python

With the news of Twitter dropping free API access we're taking a look at web scraping Twitter using Python for free. In this tutorial we'll cover two methods: using Playwright and Twitter's hidden graphql API.