Skip to content

Repository files navigation

Cloudflare Turnstile Solver

PyPI CI

Get past a Cloudflare Turnstile challenge once, then scrape with curl_cffi.

Note: intended for accessing sites you are allowed to access programmatically.

Installation

# uv
uv add cfts-solver

# pip
pip install cfts-solver

You also need Google Chrome or Chromium on the host. It runs once per host, to clear the challenge; every request after that is plain HTTP.

Use

import cfts_solver
from curl_cffi import requests

URL = "https://example.com/"

session = requests.Session(impersonate="chrome")
res = session.get(URL)

if cfts_solver.is_challenged(res.headers, res.text):
    clearance = cfts_solver.solve_challenge(URL)
    clearance.apply(session)  # cookies plus the user agent that earned them
    res = session.get(URL)

print(res.status_code, len(res.text))
from cfts_solver import TurnstileSession

with TurnstileSession() as session:
    res = session.get("https://example.com/")
    print(res.status_code)
from cfts_solver import TurnstileSolver

solver = TurnstileSolver()
clearance = solver.solve("https://example.com/")
refreshed = solver.solve("https://example.com/", refresh=True)

print(clearance.token, clearance.user_agent)

Clearances are cached under $XDG_CACHE_HOME/cfts-solver/, keyed by host and proxy, so a second process does not start a second browser. Pass ClearanceStore.in_memory() to keep nothing, or ClearanceStore(path) to keep it somewhere else.

License

MIT License

Releases

Contributors

Languages