curl-cffi

0.14.0 · active · verified Sun Mar 29

curl-cffi is a Python binding for the curl-impersonate fork via CFFI. It enables Python applications to impersonate browsers' TLS/JA3 and HTTP/2 fingerprints, effectively bypassing many anti-bot systems. The library provides a high-level API that mimics the popular `requests` library, making it intuitive to use. It supports asynchronous operations, HTTP/2, HTTP/3, and WebSockets. The current stable version is 0.14.0, with active development and frequent updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to make a basic GET request impersonating a specific browser (Chrome 110) and how to use a `Session` object for persistent connections with another impersonation profile (Safari 15.5) and cookie management. It includes basic error handling.

from curl_cffi import requests

def main():
    try:
        # Make a GET request impersonating Chrome
        response = requests.get('https://www.example.com', impersonate='chrome110')
        response.raise_for_status()  # Raise an exception for HTTP errors (4xx or 5xx)
        print(f"Status Code: {response.status_code}")
        print("Response Header:\n", response.headers)
        print("First 500 chars of Response Body:\n", response.text[:500])

        # Using a session for persistent connections and cookies
        with requests.Session() as s:
            s.impersonate = 'safari15_5'
            res_session = s.get('https://httpbin.org/cookies/set/sessioncookie/123')
            print(f"\nSession Status Code: {res_session.status_code}")
            print(f"Session Cookies: {s.cookies.get('sessioncookie')}")

    except requests.exceptions.RequestError as e:
        print(f"An error occurred: {e}")

if __name__ == '__main__':
    main()

view raw JSON →