Metadata-Version: 2.5
Name: scrapling-turnstile
Version: 0.2.1
Summary: Solve Cloudflare Turnstile in Scrapling by handing the sitekey to the Peak API and injecting the token.
Project-URL: Source, https://github.com/CircuitSavage/scrapling-turnstile
Project-URL: Homepage, https://peak.fo/?utm_source=github&utm_medium=pypi&utm_campaign=packages&utm_content=scrapling-turnstile
Project-URL: Documentation, https://peak.fo/docs/turnstile?utm_source=github&utm_medium=pypi&utm_campaign=packages&utm_content=scrapling-turnstile
Author: Peak
License: MIT
License-File: LICENSE
Keywords: anti-detect,bypass,camoufox,captcha,cloudflare,scrapling,solver,turnstile,web-scraping
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Internet :: WWW/HTTP :: Indexing/Search
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.9
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == 'dev'
Provides-Extra: scrapling
Requires-Dist: scrapling>=0.2; extra == 'scrapling'
Description-Content-Type: text/markdown

<a href="https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=scrapling-turnstile">
  <img src="https://raw.githubusercontent.com/CircuitSavage/scrapling-turnstile/main/assets/peak-banner.png" alt="Peak: solve Cloudflare Turnstile and the 5-second challenge" width="100%">
</a>

# scrapling-turnstile

Solve Cloudflare Turnstile inside a [Scrapling](https://github.com/D4Vinci/Scrapling) `StealthyFetcher` fetch. It reads the sitekey off the page, gets a token from [Peak](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=scrapling-turnstile), writes it into `cf-turnstile-response`, and fires the widget callback. The fetch clears the Turnstile/403 wall and you parse the page you asked for.

```python
from scrapling.fetchers import StealthyFetcher
from scrapling_turnstile import turnstile_action

page = StealthyFetcher.fetch(url, page_action=turnstile_action(api_key="pk_your_api_key"))
print(page.css_first("h1::text"))
```

## Why this exists

`StealthyFetcher` runs Camoufox, a fingerprint-patched Firefox. Plenty of low-risk Turnstile widgets pass on that fingerprint alone, and this package checks for that first. If `cf-turnstile-response` is already filled, you get that token for free.

The wall shows up when the widget scores the session as risky. Turnstile weighs the IP and the session history far more than the checkbox. From a datacenter IP with no trusted past, the widget holds a fetch at the "Verifying..." spinner or loops it through an invisible challenge that never resolves, and Scrapling hands you a 403 or the challenge HTML. Waiting longer does nothing, because Cloudflare's risk engine mints the token and the click does not.

So when the native pass stalls, this reads the sitekey, sends it to Peak, and injects the token Cloudflare's `siteverify` will accept. Drop in an API key and the blocked fetch keeps moving.

## Install

```bash
pip install scrapling scrapling-turnstile
scrapling install   # one-time: pull the Camoufox browser Scrapling drives
```

The package has no runtime dependencies. It drives the Scrapling page you already have.

## Quickstart

`turnstile_action()` returns a `page_action` you pass to `StealthyFetcher.fetch`. Scrapling calls it with the live page, the action solves and injects, and you get a parsed response back.

```python
import os
from scrapling.fetchers import StealthyFetcher
from scrapling_turnstile import turnstile_action

page = StealthyFetcher.fetch(
    "https://protected.example/login",
    headless=True,
    wait_selector=".cf-turnstile",       # let the widget render first
    wait_selector_state="attached",
    page_action=turnstile_action(
        api_key=os.environ["PEAK_API_KEY"],
    ),
)

print("status:", page.status)
print("title:", page.css_first("title::text"))
```

Set the key once and let it read from the environment:

```bash
export PEAK_API_KEY=pk_your_api_key   # Windows: set PEAK_API_KEY=pk_your_api_key
```

```python
page_action=turnstile_action()   # picks up PEAK_API_KEY
```

Prefer to drive the page yourself? Call the solver inside your own action:

```python
from scrapling_turnstile import solve_turnstile

def action(page):
    page.wait_for_timeout(2500)          # let the widget settle
    solve_turnstile(page, proxy="http://user:pass@host:port")
    page.click("button[type=submit]")
    return page

page = StealthyFetcher.fetch(url, page_action=action)
```

A full runnable script is in [`examples/scrapling_example.py`](./examples/scrapling_example.py).

### Async

`StealthyFetcher.async_fetch` gets an async page, so use the async twins:

```python
from scrapling.fetchers import StealthyFetcher
from scrapling_turnstile import async_turnstile_action

page = await StealthyFetcher.async_fetch(
    url,
    page_action=async_turnstile_action(api_key="pk_your_api_key"),
)
```

## Native pass vs. an API solve

Scrapling's stealth and Peak cover different failure modes. Use them together.

- **Let Scrapling try first.** `StealthyFetcher` on a clean fingerprint clears most non-interactive widgets with no solve at all. Give it a real locale and, for anything that matters, a residential or ISP `proxy`. `turnstile_action` returns that free token when it finds one, so you pay nothing on the pages that were never going to block you.
- **Solve through Peak when it stalls.** Datacenter egress, a high-risk sitekey, or an aggressive site will hold the widget open no matter how good the browser is. That is the case Peak handles: it returns a token minted against the sitekey, independent of your fetch's browser session.
- **Match the egress.** If you pass a `proxy` to Peak, use the same IP the fetch goes out on. A site is more likely to reject a token solved on one network and replayed from another.

## What it does not do

- **Not the full-page interstitial.** The Cloudflare "Just a moment" page that gates a whole domain and sets `cf_clearance` is a different challenge from an embedded Turnstile widget. This package handles the widget, the one that renders a `cf-turnstile` element and a `cf-turnstile-response` field. For the interstitial, Peak has a separate `cloudflare5stask`; see the [docs](https://peak.fo/docs/turnstile?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=scrapling-turnstile).
- **Not reCAPTCHA or hCaptcha.** Turnstile only.
- **No magic on a banned IP.** If the target already blocked your IP range, get a better egress first. A valid token on a burned network still gets refused.

## API

### `turnstile_action(api_key=None, proxy=None, sitekey=None, timeout=180.0, use_existing=True)`

Build a `page_action` for `StealthyFetcher.fetch(page_action=...)`. Solves, injects, and returns the page, which is the contract Scrapling expects. Keyword arguments match `solve_turnstile`.

### `solve_turnstile(page, api_key=None, proxy=None, sitekey=None, timeout=180.0, use_existing=True)`

Get a valid Turnstile token onto a `page` and inject it. Returns the token. Call it from inside your own `page_action` when you want to do more than solve.

- `page`: the sync Playwright page handed to your action by `StealthyFetcher.fetch`, on a Turnstile-gated URL.
- `api_key`: Peak key (`pk_...`). Falls back to `PEAK_API_KEY`.
- `proxy`: optional proxy handed to Peak, e.g. `http://user:pass@host:port`. Use the same egress as the fetch.
- `sitekey`: skip the DOM read and use this sitekey.
- `timeout`: seconds to wait on Peak.
- `use_existing`: if `True` (default), return a token Scrapling already earned instead of paying for a solve.

### `read_sitekey(page)`

Return the Turnstile sitekey rendered on the page (from `.cf-turnstile[data-sitekey]`, any `[data-sitekey]`, or the `challenges.cloudflare.com` iframe `src`), or `None`.

### `existing_token(page)`

Return the token in `cf-turnstile-response` if Scrapling already passed the widget, else `None`.

### `inject_token(page, token)`

Write `token` into the response field(s) and fire the widget's `data-callback`. Returns `True` if a callback was found and called.

### `request_token(sitekey, url, api_key=None, proxy=None, timeout=180.0)`

The raw, blocking Peak call, in case you want the token without a page. Returns the token string or raises `PeakError`.

**Async twins:** `async_turnstile_action`, `async_solve_turnstile`, `async_read_sitekey`, `async_existing_token`, `async_inject_token`. Same signatures, for `StealthyFetcher.async_fetch`.

## How it works

1. If `use_existing`, read `cf-turnstile-response`; a filled field means Scrapling already passed, so return that token and skip the solve.
2. Read the sitekey from the widget, or scrape it from the Cloudflare iframe `src` when the widget rendered implicitly.
3. Call Peak `POST https://api.peak.fo/solve` with `task_type: "turnstiletask"`, the `sitekey`, `url: page.url`, and your optional `proxy`, authenticated with the `X-API-Key` header. Peak returns `{"success": true, "data": {"token": "..."}}`.
4. Inject the token into every `cf-turnstile-response` field (creating a hidden input if the widget hasn't rendered one) and invoke the widget's `data-callback`, so the host page's success handler runs.

The sync path calls Peak inline, which is fine inside a synchronous `fetch` action. The async path runs the blocking call in a worker thread so the browser's event loop keeps ticking.

## Earn with your app ID

Set the `PEAK_APP_ID` environment variable to earn 5% of every solve this package routes to Peak, paid as Peak solve credit. It is optional, works across every entry point (sync and async), and does not change whether a solve succeeds or how fast it returns.

```bash
export PEAK_APP_ID=app_your_id   # Windows: set PEAK_APP_ID=app_your_id
```

Create an app id at [peak.fo/dashboard/developer](https://peak.fo/dashboard/developer); details at [peak.fo/earn](https://peak.fo/earn).

## Powered by Peak

This uses [Peak](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=scrapling-turnstile), a solver built for Cloudflare Turnstile and the 5-second challenge.

- **$0.90 per 1,000** successful Turnstile solves, down to **$0.35 at volume**.
- **Pay only for successes.** Peak does not bill a failed solve.
- **1,000 free solves** to start, no card.

→ [Get a free API key](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=scrapling-turnstile) · [Turnstile docs](https://peak.fo/docs/turnstile?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=scrapling-turnstile) · [Pricing](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=scrapling-turnstile#pricing)

## Troubleshooting

Every failure raises `PeakError`. The message tells you which step broke.

- **`PeakError: No Turnstile sitekey on the page. Has the widget rendered yet?`** The action ran before the widget existed. Pass `wait_selector=".cf-turnstile"` and `wait_selector_state="attached"` to `StealthyFetcher.fetch`, as the quickstart does. A widget built by `turnstile.render()` with no `data-sitekey` attribute still exposes the key in the `challenges.cloudflare.com` iframe `src`, so wait for that iframe. If you know the key, pass `sitekey=`. [How to find a Cloudflare Turnstile sitekey](https://blog.peak.fo/how-to-find-a-cloudflare-turnstile-sitekey/) shows where sites hide it.
- **`PeakError: No Peak API key.`** Neither `api_key=` nor `PEAK_API_KEY` held a key. Keys start with `pk_`; the free one comes from the [dashboard](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=scrapling-turnstile).
- **`PeakError: Peak HTTP <status>: ...`** Peak refused the request, and the body after the colon is Peak's own reason. Check the key and the balance first.
- **`PeakError: Could not reach Peak: ...`** The request never left your network. Check DNS, the firewall, and any `HTTPS_PROXY` variable in the environment, since `urllib` honours it.
- **`PeakError: Solve failed: ...`** Peak answered with `success: false` and did not bill the solve. Common causes: a sitekey that does not belong to `page.url`, or a `proxy=` Peak could not connect through. The [Cloudflare Turnstile error codes](https://blog.peak.fo/cloudflare-turnstile-error-codes/) post covers the widget-side codes (110200, 300030, 600010 and the rest).
- **The token is in the field and the page does not move.** `inject_token` returns `True` only when it found a `data-callback` function and called it. A site that passes a closure to `turnstile.render({callback: ...})` keeps that function out of reach. Write your own `page_action`, call `solve_turnstile(page)`, then click the submit button. [cf-turnstile-response is empty](https://blog.peak.fo/cf-turnstile-response-empty/) and [the cf-turnstile-response token explained](https://blog.peak.fo/the-cf-turnstile-response-token-explained/) cover the field.
- **`page.status` is still 403 after the action.** The status belongs to the first navigation, which happened before the solve. Submit the form or navigate again inside your `page_action` and read the page you land on. If the site refuses the token itself, check its age (single-use, 300 seconds) and pass `proxy=` with the fetch's own egress. [Cloudflare Turnstile 403](https://blog.peak.fo/cloudflare-turnstile-403-scraper-fix/) covers the refusal.
- **The fetch lands on "Just a moment..." with no widget.** That page is the managed 5-second challenge, a separate check that wants a `cf_clearance` cookie. [Turnstile token vs cookie](https://blog.peak.fo/cloudflare-turnstile-token-vs-cookie/) explains the difference, and [the Cloudflare 5-second challenge explained](https://blog.peak.fo/the-cloudflare-5-second-challenge-explained/) covers the interstitial.

Two longer reads for this stack: [How to scrape Cloudflare-protected sites without getting blocked](https://blog.peak.fo/how-to-scrape-cloudflare-protected-sites-without-getting-blocked/) (also on [Hashnode](https://peakfo.hashnode.dev/how-to-scrape-cloudflare-protected-sites-without-getting-blocked)) and the [2026 Turnstile developer guide](https://blog.peak.fo/how-to-solve-cloudflare-turnstile-in-2026-dev-guide/).

## Related

Same solve, wired into other stacks: [camoufox-turnstile](https://github.com/CircuitSavage/camoufox-turnstile) (the browser under Scrapling's `StealthyFetcher`) · [playwright-turnstile](https://github.com/CircuitSavage/playwright-turnstile) · [patchright-turnstile](https://github.com/CircuitSavage/patchright-turnstile) · [nodriver-turnstile](https://github.com/CircuitSavage/nodriver-turnstile) · [pydoll-turnstile](https://github.com/CircuitSavage/pydoll-turnstile) · [drissionpage-turnstile](https://github.com/CircuitSavage/drissionpage-turnstile) · [selenium-turnstile](https://github.com/CircuitSavage/selenium-turnstile) · [botasaurus-turnstile](https://github.com/CircuitSavage/botasaurus-turnstile) · [scrapy-turnstile](https://github.com/CircuitSavage/scrapy-turnstile) · [cloudscraper-turnstile](https://github.com/CircuitSavage/cloudscraper-turnstile) (a `requests.Session` drop-in, no browser) · [turnstile-curl](https://github.com/CircuitSavage/turnstile-curl) · [crawlee-turnstile](https://github.com/CircuitSavage/crawlee-turnstile) · [puppeteer-extra-plugin-turnstile](https://github.com/CircuitSavage/puppeteer-extra-plugin-turnstile) · [browser-use-turnstile](https://github.com/CircuitSavage/browser-use-turnstile) · [crawl4ai-turnstile](https://github.com/CircuitSavage/crawl4ai-turnstile). Full list in [awesome-turnstile-solvers](https://github.com/CircuitSavage/awesome-turnstile-solvers).

[cloudflare-turnstile-reversed](https://github.com/CircuitSavage/cloudflare-turnstile-reversed) documents the challenge flow, the widget parameters and the fingerprinting the widget runs before it hands out a token.

## Legitimate use

Built for automation, QA, and scraping public data. Respect each site's Terms of Service and `robots.txt`, and don't point it at credential-stuffing or other abuse. How you use it is on you.

## License

MIT. See [LICENSE](./LICENSE).
