cloudscraper-turnstile 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cloudscraper_turnstile-0.1.0/LICENSE +21 -0
- cloudscraper_turnstile-0.1.0/PKG-INFO +216 -0
- cloudscraper_turnstile-0.1.0/README.md +186 -0
- cloudscraper_turnstile-0.1.0/TEST.md +90 -0
- cloudscraper_turnstile-0.1.0/assets/peak-banner.png +0 -0
- cloudscraper_turnstile-0.1.0/cloudscraper_turnstile/__init__.py +46 -0
- cloudscraper_turnstile-0.1.0/cloudscraper_turnstile/detect.py +114 -0
- cloudscraper_turnstile-0.1.0/cloudscraper_turnstile/peak.py +158 -0
- cloudscraper_turnstile-0.1.0/cloudscraper_turnstile/scraper.py +256 -0
- cloudscraper_turnstile-0.1.0/examples/basic.py +38 -0
- cloudscraper_turnstile-0.1.0/examples/migration_from_cloudscraper.py +51 -0
- cloudscraper_turnstile-0.1.0/pyproject.toml +65 -0
- cloudscraper_turnstile-0.1.0/tests/test_scraper.py +345 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Peak
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: cloudscraper-turnstile
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: cloudscraper drop-in that actually solves Cloudflare Turnstile and the 5s interstitial via the Peak API. Subclasses requests.Session; swap two lines and your existing scraper works.
|
|
5
|
+
Project-URL: Homepage, https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile
|
|
6
|
+
Project-URL: Documentation, https://peak.fo/docs/turnstile?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile
|
|
7
|
+
Project-URL: Pricing, https://peak.fo/pricing?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile
|
|
8
|
+
Project-URL: Source, https://github.com/CircuitSavage/cloudscraper-turnstile
|
|
9
|
+
Author: Peak
|
|
10
|
+
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: 403,anti-bot,bypass,captcha,captcha-solver,cloudflare,cloudflare-bypass,cloudflare-turnstile,cloudscraper,cloudscraper-alternative,requests,scraper,turnstile,web-scraping
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
25
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Indexing/Search
|
|
26
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
27
|
+
Requires-Python: >=3.8
|
|
28
|
+
Requires-Dist: requests>=2.25
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
<a href="https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile">
|
|
32
|
+
<img src="https://raw.githubusercontent.com/CircuitSavage/cloudscraper-turnstile/main/assets/peak-banner.png" alt="Peak — solve Cloudflare Turnstile & the 5s challenge in ~1s" width="100%">
|
|
33
|
+
</a>
|
|
34
|
+
|
|
35
|
+
# cloudscraper-turnstile
|
|
36
|
+
|
|
37
|
+
**cloudscraper-turnstile** is a drop-in replacement for [cloudscraper](https://github.com/VeNoMouS/cloudscraper) that actually solves Cloudflare Turnstile and the 5s "Just a moment..." interstitial. It subclasses `requests.Session` exactly like cloudscraper does, so you change one import line and your existing scraper keeps working, except now the requests that used to return `403` come back with the real page.
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import cloudscraper_turnstile as cloudscraper
|
|
41
|
+
|
|
42
|
+
scraper = cloudscraper.create_scraper(api_key="pk_your_api_key")
|
|
43
|
+
resp = scraper.get("https://protected.example.com/") # Turnstile solved, real page returned
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Why cloudscraper can't do this anymore
|
|
47
|
+
|
|
48
|
+
cloudscraper is a good tool for the challenge it was built for, but that challenge is mostly gone:
|
|
49
|
+
|
|
50
|
+
- **It only ever solved the legacy IUAM challenge.** cloudscraper works by reading Cloudflare's old "I'm Under Attack Mode" page, extracting the JavaScript math problem, and evaluating it. That is the entire mechanism.
|
|
51
|
+
- **It has no JS engine and no browser.** Modern Cloudflare managed challenges and Turnstile run real, obfuscated browser JavaScript and collect behavioral and fingerprint signals. There is nothing in the page for a regex-and-eval tool to solve, so the request stays on the challenge page and you get a `403`, a `503`, or an endless "Just a moment..." loop.
|
|
52
|
+
- **Its own "Turnstile support" is a hand-off.** cloudscraper never solved Turnstile itself. It added hooks to pass the challenge to a third-party paid CAPTCHA API and inject the returned token. Without one of those API keys configured, Turnstile support does nothing.
|
|
53
|
+
- **The project is effectively unmaintained.** Its last real release was in 2023. Cloudflare has shipped many detection changes since.
|
|
54
|
+
|
|
55
|
+
So on any site that switched to Turnstile or the managed 5s challenge (most of them, by now), plain cloudscraper returns the challenge HTML instead of your data. This package fills exactly that gap: it keeps cloudscraper's `create_scraper` / `requests.Session` shape and does the one thing cloudscraper cannot, by sending the challenge to a solver that runs a real browser.
|
|
56
|
+
|
|
57
|
+
## Powered by Peak
|
|
58
|
+
|
|
59
|
+
This package uses [Peak](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile) to solve Turnstile and the 5s challenge.
|
|
60
|
+
|
|
61
|
+
- Solve Cloudflare Turnstile & the 5s challenge in about a second
|
|
62
|
+
- Pay only for successful solves, from $1 / 1,000
|
|
63
|
+
- 1,000 free solves to start, no card.
|
|
64
|
+
|
|
65
|
+
→ [Get your free API key](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile) · [Docs](https://peak.fo/docs/turnstile?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile) · [Pricing](https://peak.fo/pricing?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile)
|
|
66
|
+
|
|
67
|
+
## Install
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
pip install cloudscraper-turnstile
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The only dependency is `requests` (you already have it if you used cloudscraper).
|
|
74
|
+
|
|
75
|
+
## Migration guide
|
|
76
|
+
|
|
77
|
+
There is no rewrite. cloudscraper-turnstile mirrors cloudscraper's public API: `create_scraper(...)` returns a `CloudScraper` object that subclasses `requests.Session`, so `.get`, `.post`, `.request`, `.cookies`, and `.headers` all behave the same.
|
|
78
|
+
|
|
79
|
+
**Before**
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
import cloudscraper
|
|
83
|
+
|
|
84
|
+
scraper = cloudscraper.create_scraper()
|
|
85
|
+
resp = scraper.get("https://protected.example.com/") # 403 / "Just a moment..." on Turnstile sites
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**After**
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
import cloudscraper_turnstile as cloudscraper # the only line that changes
|
|
92
|
+
|
|
93
|
+
scraper = cloudscraper.create_scraper() # reads PEAK_API_KEY from env
|
|
94
|
+
resp = scraper.get("https://protected.example.com/") # Turnstile solved, real page returned
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
If you were already using cloudscraper's captcha-provider style, that keeps working too, so migration is a no-op:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
scraper = cloudscraper.create_scraper(
|
|
101
|
+
captcha={"provider": "peak", "api_key": "pk_your_api_key"},
|
|
102
|
+
)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Existing cloudscraper kwargs (`browser`, `delay`, `interpreter`, `allow_brotli`, `sess`, ...) are accepted and never raise, so you do not have to touch the rest of your `create_scraper` call.
|
|
106
|
+
|
|
107
|
+
## Quickstart
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
pip install cloudscraper-turnstile requests
|
|
111
|
+
export PEAK_API_KEY=pk_your_api_key # Windows: setx PEAK_API_KEY pk_your_api_key
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
import os
|
|
116
|
+
import cloudscraper_turnstile as cloudscraper
|
|
117
|
+
|
|
118
|
+
scraper = cloudscraper.create_scraper(api_key=os.environ["PEAK_API_KEY"])
|
|
119
|
+
|
|
120
|
+
# Use it exactly like requests / cloudscraper. A Turnstile or 5s challenge is
|
|
121
|
+
# detected, solved through Peak, and the request is retried transparently.
|
|
122
|
+
resp = scraper.get("https://protected.example.com/")
|
|
123
|
+
print(resp.status_code) # 200
|
|
124
|
+
print(resp.text) # the real page, not the challenge
|
|
125
|
+
|
|
126
|
+
# Cookies obtained during the solve (including cf_clearance) persist on the
|
|
127
|
+
# session for every later request.
|
|
128
|
+
resp2 = scraper.post(
|
|
129
|
+
"https://protected.example.com/api/search",
|
|
130
|
+
json={"q": "widgets"},
|
|
131
|
+
)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Where the key comes from
|
|
135
|
+
|
|
136
|
+
The API key is resolved in this order:
|
|
137
|
+
|
|
138
|
+
1. Explicit `create_scraper(api_key="pk_...")`
|
|
139
|
+
2. `create_scraper(captcha={"provider": "peak", "api_key": "pk_..."})`
|
|
140
|
+
3. `PEAK_API_KEY` environment variable
|
|
141
|
+
|
|
142
|
+
### Using a proxy
|
|
143
|
+
|
|
144
|
+
Pass a proxy and it is forwarded to Peak, so the solve happens from the same IP as your crawl (this matters, since Cloudflare ties clearance to the requesting IP):
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
scraper = cloudscraper.create_scraper(
|
|
148
|
+
api_key="pk_your_api_key",
|
|
149
|
+
proxy="http://user:pass@ip:port",
|
|
150
|
+
)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
If you set `scraper.proxies` the requests way instead, that proxy is used for the solve automatically.
|
|
154
|
+
|
|
155
|
+
## Compatibility
|
|
156
|
+
|
|
157
|
+
| cloudscraper API | Status in cloudscraper-turnstile |
|
|
158
|
+
| --- | --- |
|
|
159
|
+
| `create_scraper(**kwargs)` | Supported. Returns a `requests.Session` subclass. |
|
|
160
|
+
| Returns a `requests.Session` | Yes. `CloudScraper(requests.Session)`. |
|
|
161
|
+
| `.get / .post / .put / .delete / .request` | Supported (inherited from `requests.Session`). |
|
|
162
|
+
| `.cookies`, `.headers`, `.proxies`, `.auth` | Supported (session state persists across the solve retry). |
|
|
163
|
+
| `sess=` (adopt an existing Session) | Supported (cookies, headers, proxies, auth carried over). |
|
|
164
|
+
| `captcha={"provider": ..., "api_key": ...}` | Supported. `provider: "peak"` is native; the `api_key` is read regardless. |
|
|
165
|
+
| `browser=`, `delay=`, `interpreter=`, `allow_brotli=`, `debug=` | Accepted, never raise. `debug=True` prints solve steps. |
|
|
166
|
+
| Legacy IUAM JS-math challenge | Handled by Cloudflare's own retry path; not the focus. |
|
|
167
|
+
| Cloudflare Turnstile | **Solved** via Peak (`turnstiletask`). |
|
|
168
|
+
| Cloudflare 5s "Just a moment" / managed | **Solved** via Peak (`cloudflare5stask`). |
|
|
169
|
+
|
|
170
|
+
## cloudscraper vs cloudscraper-turnstile
|
|
171
|
+
|
|
172
|
+
| Challenge / trait | cloudscraper | cloudscraper-turnstile |
|
|
173
|
+
| --- | --- | --- |
|
|
174
|
+
| Legacy IUAM JS-math | Yes | Yes |
|
|
175
|
+
| Cloudflare Turnstile | No (hands off to a paid API you must wire up) | **Yes** |
|
|
176
|
+
| 5s "Just a moment" interstitial | No (no JS engine) | **Yes** |
|
|
177
|
+
| Managed challenge | No | **Yes** |
|
|
178
|
+
| `requests.Session` drop-in | Yes | Yes |
|
|
179
|
+
| Actively maintained | No (last release 2023) | Yes |
|
|
180
|
+
|
|
181
|
+
## FAQ
|
|
182
|
+
|
|
183
|
+
**Why is cloudscraper not working in 2026?**
|
|
184
|
+
Because the site moved to Cloudflare Turnstile or the managed 5s challenge. cloudscraper only solves the old IUAM JavaScript-math page and has no JS engine, so on a modern challenge it returns the challenge HTML (usually `403`, `503`, or a "Just a moment..." page) instead of your data. Its last release was in 2023. cloudscraper-turnstile detects those challenges and solves them through Peak.
|
|
185
|
+
|
|
186
|
+
**What is a good cloudscraper alternative for Turnstile?**
|
|
187
|
+
cloudscraper-turnstile is built to be that alternative: same `create_scraper` API, same `requests.Session` object, but it actually solves Turnstile. Change the import, add a Peak API key, and your existing code runs.
|
|
188
|
+
|
|
189
|
+
**How do I fix cloudscraper 403 Forbidden?**
|
|
190
|
+
A `403` from a Cloudflare-protected site usually means you received the challenge page, not a real block. Confirm the body contains `cf-turnstile`, `Just a moment`, or `cf_chl_opt`. If so, switch to `import cloudscraper_turnstile as cloudscraper`, set `PEAK_API_KEY`, and the challenge is solved and the request retried automatically. If the proxy/IP that gets challenged is not your default one, pass `proxy=` so the solve matches.
|
|
191
|
+
|
|
192
|
+
**Does cloudscraper solve Cloudflare Turnstile?**
|
|
193
|
+
Not on its own. cloudscraper never solved Turnstile itself; it only added hooks to pass the token from an external paid CAPTCHA service. cloudscraper-turnstile does the solve for you through Peak and injects the `cf-turnstile-response` token, so there is nothing else to wire up.
|
|
194
|
+
|
|
195
|
+
**Do I need a real browser or Selenium?**
|
|
196
|
+
No. This is a pure `requests`-based client. The browser work happens on Peak's side; you send the sitekey and URL and get a token (or a `cf_clearance` cookie for the 5s challenge) back.
|
|
197
|
+
|
|
198
|
+
**Does it handle the 5s "Just a moment" page too?**
|
|
199
|
+
Yes. When the response is the interstitial rather than a Turnstile widget, the package calls Peak's `cloudflare5stask`, sets the returned `cf_clearance` cookie (and user-agent) on the session, and re-requests the original URL.
|
|
200
|
+
|
|
201
|
+
**Will it loop forever if a solve fails?**
|
|
202
|
+
No. Solving is capped by `max_solve_attempts` (default 3). After that the last response is returned as-is so you can inspect it.
|
|
203
|
+
|
|
204
|
+
## Legitimate use
|
|
205
|
+
|
|
206
|
+
Use this for automation, QA, monitoring, and scraping public data you are allowed to access. Respect each target's Terms of Service and `robots.txt`, rate-limit yourself, and do not use it for credential stuffing or to access data you have no right to. You are responsible for how you use it.
|
|
207
|
+
|
|
208
|
+
## Links
|
|
209
|
+
|
|
210
|
+
- [Peak home](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile)
|
|
211
|
+
- [Turnstile docs](https://peak.fo/docs/turnstile?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile)
|
|
212
|
+
- [Pricing](https://peak.fo/pricing?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile)
|
|
213
|
+
|
|
214
|
+
## License
|
|
215
|
+
|
|
216
|
+
MIT. See [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
<a href="https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/CircuitSavage/cloudscraper-turnstile/main/assets/peak-banner.png" alt="Peak — solve Cloudflare Turnstile & the 5s challenge in ~1s" width="100%">
|
|
3
|
+
</a>
|
|
4
|
+
|
|
5
|
+
# cloudscraper-turnstile
|
|
6
|
+
|
|
7
|
+
**cloudscraper-turnstile** is a drop-in replacement for [cloudscraper](https://github.com/VeNoMouS/cloudscraper) that actually solves Cloudflare Turnstile and the 5s "Just a moment..." interstitial. It subclasses `requests.Session` exactly like cloudscraper does, so you change one import line and your existing scraper keeps working, except now the requests that used to return `403` come back with the real page.
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
import cloudscraper_turnstile as cloudscraper
|
|
11
|
+
|
|
12
|
+
scraper = cloudscraper.create_scraper(api_key="pk_your_api_key")
|
|
13
|
+
resp = scraper.get("https://protected.example.com/") # Turnstile solved, real page returned
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Why cloudscraper can't do this anymore
|
|
17
|
+
|
|
18
|
+
cloudscraper is a good tool for the challenge it was built for, but that challenge is mostly gone:
|
|
19
|
+
|
|
20
|
+
- **It only ever solved the legacy IUAM challenge.** cloudscraper works by reading Cloudflare's old "I'm Under Attack Mode" page, extracting the JavaScript math problem, and evaluating it. That is the entire mechanism.
|
|
21
|
+
- **It has no JS engine and no browser.** Modern Cloudflare managed challenges and Turnstile run real, obfuscated browser JavaScript and collect behavioral and fingerprint signals. There is nothing in the page for a regex-and-eval tool to solve, so the request stays on the challenge page and you get a `403`, a `503`, or an endless "Just a moment..." loop.
|
|
22
|
+
- **Its own "Turnstile support" is a hand-off.** cloudscraper never solved Turnstile itself. It added hooks to pass the challenge to a third-party paid CAPTCHA API and inject the returned token. Without one of those API keys configured, Turnstile support does nothing.
|
|
23
|
+
- **The project is effectively unmaintained.** Its last real release was in 2023. Cloudflare has shipped many detection changes since.
|
|
24
|
+
|
|
25
|
+
So on any site that switched to Turnstile or the managed 5s challenge (most of them, by now), plain cloudscraper returns the challenge HTML instead of your data. This package fills exactly that gap: it keeps cloudscraper's `create_scraper` / `requests.Session` shape and does the one thing cloudscraper cannot, by sending the challenge to a solver that runs a real browser.
|
|
26
|
+
|
|
27
|
+
## Powered by Peak
|
|
28
|
+
|
|
29
|
+
This package uses [Peak](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile) to solve Turnstile and the 5s challenge.
|
|
30
|
+
|
|
31
|
+
- Solve Cloudflare Turnstile & the 5s challenge in about a second
|
|
32
|
+
- Pay only for successful solves, from $1 / 1,000
|
|
33
|
+
- 1,000 free solves to start, no card.
|
|
34
|
+
|
|
35
|
+
→ [Get your free API key](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile) · [Docs](https://peak.fo/docs/turnstile?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile) · [Pricing](https://peak.fo/pricing?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile)
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install cloudscraper-turnstile
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The only dependency is `requests` (you already have it if you used cloudscraper).
|
|
44
|
+
|
|
45
|
+
## Migration guide
|
|
46
|
+
|
|
47
|
+
There is no rewrite. cloudscraper-turnstile mirrors cloudscraper's public API: `create_scraper(...)` returns a `CloudScraper` object that subclasses `requests.Session`, so `.get`, `.post`, `.request`, `.cookies`, and `.headers` all behave the same.
|
|
48
|
+
|
|
49
|
+
**Before**
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
import cloudscraper
|
|
53
|
+
|
|
54
|
+
scraper = cloudscraper.create_scraper()
|
|
55
|
+
resp = scraper.get("https://protected.example.com/") # 403 / "Just a moment..." on Turnstile sites
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**After**
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
import cloudscraper_turnstile as cloudscraper # the only line that changes
|
|
62
|
+
|
|
63
|
+
scraper = cloudscraper.create_scraper() # reads PEAK_API_KEY from env
|
|
64
|
+
resp = scraper.get("https://protected.example.com/") # Turnstile solved, real page returned
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
If you were already using cloudscraper's captcha-provider style, that keeps working too, so migration is a no-op:
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
scraper = cloudscraper.create_scraper(
|
|
71
|
+
captcha={"provider": "peak", "api_key": "pk_your_api_key"},
|
|
72
|
+
)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Existing cloudscraper kwargs (`browser`, `delay`, `interpreter`, `allow_brotli`, `sess`, ...) are accepted and never raise, so you do not have to touch the rest of your `create_scraper` call.
|
|
76
|
+
|
|
77
|
+
## Quickstart
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
pip install cloudscraper-turnstile requests
|
|
81
|
+
export PEAK_API_KEY=pk_your_api_key # Windows: setx PEAK_API_KEY pk_your_api_key
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
import os
|
|
86
|
+
import cloudscraper_turnstile as cloudscraper
|
|
87
|
+
|
|
88
|
+
scraper = cloudscraper.create_scraper(api_key=os.environ["PEAK_API_KEY"])
|
|
89
|
+
|
|
90
|
+
# Use it exactly like requests / cloudscraper. A Turnstile or 5s challenge is
|
|
91
|
+
# detected, solved through Peak, and the request is retried transparently.
|
|
92
|
+
resp = scraper.get("https://protected.example.com/")
|
|
93
|
+
print(resp.status_code) # 200
|
|
94
|
+
print(resp.text) # the real page, not the challenge
|
|
95
|
+
|
|
96
|
+
# Cookies obtained during the solve (including cf_clearance) persist on the
|
|
97
|
+
# session for every later request.
|
|
98
|
+
resp2 = scraper.post(
|
|
99
|
+
"https://protected.example.com/api/search",
|
|
100
|
+
json={"q": "widgets"},
|
|
101
|
+
)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Where the key comes from
|
|
105
|
+
|
|
106
|
+
The API key is resolved in this order:
|
|
107
|
+
|
|
108
|
+
1. Explicit `create_scraper(api_key="pk_...")`
|
|
109
|
+
2. `create_scraper(captcha={"provider": "peak", "api_key": "pk_..."})`
|
|
110
|
+
3. `PEAK_API_KEY` environment variable
|
|
111
|
+
|
|
112
|
+
### Using a proxy
|
|
113
|
+
|
|
114
|
+
Pass a proxy and it is forwarded to Peak, so the solve happens from the same IP as your crawl (this matters, since Cloudflare ties clearance to the requesting IP):
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
scraper = cloudscraper.create_scraper(
|
|
118
|
+
api_key="pk_your_api_key",
|
|
119
|
+
proxy="http://user:pass@ip:port",
|
|
120
|
+
)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
If you set `scraper.proxies` the requests way instead, that proxy is used for the solve automatically.
|
|
124
|
+
|
|
125
|
+
## Compatibility
|
|
126
|
+
|
|
127
|
+
| cloudscraper API | Status in cloudscraper-turnstile |
|
|
128
|
+
| --- | --- |
|
|
129
|
+
| `create_scraper(**kwargs)` | Supported. Returns a `requests.Session` subclass. |
|
|
130
|
+
| Returns a `requests.Session` | Yes. `CloudScraper(requests.Session)`. |
|
|
131
|
+
| `.get / .post / .put / .delete / .request` | Supported (inherited from `requests.Session`). |
|
|
132
|
+
| `.cookies`, `.headers`, `.proxies`, `.auth` | Supported (session state persists across the solve retry). |
|
|
133
|
+
| `sess=` (adopt an existing Session) | Supported (cookies, headers, proxies, auth carried over). |
|
|
134
|
+
| `captcha={"provider": ..., "api_key": ...}` | Supported. `provider: "peak"` is native; the `api_key` is read regardless. |
|
|
135
|
+
| `browser=`, `delay=`, `interpreter=`, `allow_brotli=`, `debug=` | Accepted, never raise. `debug=True` prints solve steps. |
|
|
136
|
+
| Legacy IUAM JS-math challenge | Handled by Cloudflare's own retry path; not the focus. |
|
|
137
|
+
| Cloudflare Turnstile | **Solved** via Peak (`turnstiletask`). |
|
|
138
|
+
| Cloudflare 5s "Just a moment" / managed | **Solved** via Peak (`cloudflare5stask`). |
|
|
139
|
+
|
|
140
|
+
## cloudscraper vs cloudscraper-turnstile
|
|
141
|
+
|
|
142
|
+
| Challenge / trait | cloudscraper | cloudscraper-turnstile |
|
|
143
|
+
| --- | --- | --- |
|
|
144
|
+
| Legacy IUAM JS-math | Yes | Yes |
|
|
145
|
+
| Cloudflare Turnstile | No (hands off to a paid API you must wire up) | **Yes** |
|
|
146
|
+
| 5s "Just a moment" interstitial | No (no JS engine) | **Yes** |
|
|
147
|
+
| Managed challenge | No | **Yes** |
|
|
148
|
+
| `requests.Session` drop-in | Yes | Yes |
|
|
149
|
+
| Actively maintained | No (last release 2023) | Yes |
|
|
150
|
+
|
|
151
|
+
## FAQ
|
|
152
|
+
|
|
153
|
+
**Why is cloudscraper not working in 2026?**
|
|
154
|
+
Because the site moved to Cloudflare Turnstile or the managed 5s challenge. cloudscraper only solves the old IUAM JavaScript-math page and has no JS engine, so on a modern challenge it returns the challenge HTML (usually `403`, `503`, or a "Just a moment..." page) instead of your data. Its last release was in 2023. cloudscraper-turnstile detects those challenges and solves them through Peak.
|
|
155
|
+
|
|
156
|
+
**What is a good cloudscraper alternative for Turnstile?**
|
|
157
|
+
cloudscraper-turnstile is built to be that alternative: same `create_scraper` API, same `requests.Session` object, but it actually solves Turnstile. Change the import, add a Peak API key, and your existing code runs.
|
|
158
|
+
|
|
159
|
+
**How do I fix cloudscraper 403 Forbidden?**
|
|
160
|
+
A `403` from a Cloudflare-protected site usually means you received the challenge page, not a real block. Confirm the body contains `cf-turnstile`, `Just a moment`, or `cf_chl_opt`. If so, switch to `import cloudscraper_turnstile as cloudscraper`, set `PEAK_API_KEY`, and the challenge is solved and the request retried automatically. If the proxy/IP that gets challenged is not your default one, pass `proxy=` so the solve matches.
|
|
161
|
+
|
|
162
|
+
**Does cloudscraper solve Cloudflare Turnstile?**
|
|
163
|
+
Not on its own. cloudscraper never solved Turnstile itself; it only added hooks to pass the token from an external paid CAPTCHA service. cloudscraper-turnstile does the solve for you through Peak and injects the `cf-turnstile-response` token, so there is nothing else to wire up.
|
|
164
|
+
|
|
165
|
+
**Do I need a real browser or Selenium?**
|
|
166
|
+
No. This is a pure `requests`-based client. The browser work happens on Peak's side; you send the sitekey and URL and get a token (or a `cf_clearance` cookie for the 5s challenge) back.
|
|
167
|
+
|
|
168
|
+
**Does it handle the 5s "Just a moment" page too?**
|
|
169
|
+
Yes. When the response is the interstitial rather than a Turnstile widget, the package calls Peak's `cloudflare5stask`, sets the returned `cf_clearance` cookie (and user-agent) on the session, and re-requests the original URL.
|
|
170
|
+
|
|
171
|
+
**Will it loop forever if a solve fails?**
|
|
172
|
+
No. Solving is capped by `max_solve_attempts` (default 3). After that the last response is returned as-is so you can inspect it.
|
|
173
|
+
|
|
174
|
+
## Legitimate use
|
|
175
|
+
|
|
176
|
+
Use this for automation, QA, monitoring, and scraping public data you are allowed to access. Respect each target's Terms of Service and `robots.txt`, rate-limit yourself, and do not use it for credential stuffing or to access data you have no right to. You are responsible for how you use it.
|
|
177
|
+
|
|
178
|
+
## Links
|
|
179
|
+
|
|
180
|
+
- [Peak home](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile)
|
|
181
|
+
- [Turnstile docs](https://peak.fo/docs/turnstile?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile)
|
|
182
|
+
- [Pricing](https://peak.fo/pricing?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=cloudscraper-turnstile)
|
|
183
|
+
|
|
184
|
+
## License
|
|
185
|
+
|
|
186
|
+
MIT. See [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# Testing
|
|
2
|
+
|
|
3
|
+
The suite uses only the Python standard library (`unittest` + `unittest.mock`).
|
|
4
|
+
It requires **no live Peak key and no network**:
|
|
5
|
+
|
|
6
|
+
- The Peak client's single HTTP method (`PeakClient._post`) is monkeypatched to
|
|
7
|
+
return `{"success": true, "data": {"token": "XXXX.TEST"}}` for Turnstile and a
|
|
8
|
+
`{"success": true, "data": {"cookies": {"cf_clearance": "CLEAR.TEST"}, ...}}`
|
|
9
|
+
shape for the 5s task.
|
|
10
|
+
- `requests.Session.request` (the transport the `CloudScraper` calls via
|
|
11
|
+
`super().request`) is replaced with a scripted fake that returns a challenge
|
|
12
|
+
first and the real page on retry.
|
|
13
|
+
|
|
14
|
+
The only real dependency is `requests` (a hard dependency of the package, since
|
|
15
|
+
a cloudscraper drop-in always has it):
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install requests
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## What is asserted
|
|
22
|
+
|
|
23
|
+
`tests/test_scraper.py` covers the full drop-in contract:
|
|
24
|
+
|
|
25
|
+
- **(a) Type contract** - `create_scraper()` returns a `requests.Session`
|
|
26
|
+
subclass (`CloudScraper`); `import cloudscraper_turnstile as cloudscraper` then
|
|
27
|
+
`cloudscraper.create_scraper()` works; `.get/.post/.request/.cookies/.headers`
|
|
28
|
+
are present.
|
|
29
|
+
- **(b) Pass-through** - a non-challenge 200 response is returned unchanged and
|
|
30
|
+
Peak is never called.
|
|
31
|
+
- **(c) Turnstile solve + retry** - a 403 Turnstile page triggers a Peak call
|
|
32
|
+
with the correct body (`task_type=turnstiletask`, extracted `sitekey`, `url`),
|
|
33
|
+
the token is injected as the `cf-turnstile-response` form field, the challenge
|
|
34
|
+
form is resubmitted, and the final real page is returned.
|
|
35
|
+
- **(d) 5s interstitial** - a 503 "Just a moment" page triggers a
|
|
36
|
+
`cloudflare5stask` call (no sitekey), the returned `cf_clearance` cookie and
|
|
37
|
+
user-agent are set on the session, and the original URL is re-requested.
|
|
38
|
+
- **(e) API-key precedence** - explicit `api_key=` > `captcha={'provider':'peak',
|
|
39
|
+
'api_key':...}` > `PEAK_API_KEY` env, each honored.
|
|
40
|
+
- **(f) Proxy forwarding** - an explicit `proxy=` and, failing that, the
|
|
41
|
+
session's configured proxy are forwarded to Peak so the solve matches the
|
|
42
|
+
crawl IP.
|
|
43
|
+
- **(g) Drop-in kwargs** - unknown cloudscraper kwargs (`browser`, `delay`,
|
|
44
|
+
`interpreter`, `allow_brotli`, future kwargs) do not raise; `sess=` state is
|
|
45
|
+
adopted.
|
|
46
|
+
- **(h) Loop guard** - when every response is a challenge, the solver stops
|
|
47
|
+
after `max_solve_attempts` instead of looping forever.
|
|
48
|
+
|
|
49
|
+
## Run
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
cd cloudscraper-turnstile
|
|
53
|
+
pip install requests
|
|
54
|
+
python -m unittest discover -s tests -v
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Python used: 3.12
|
|
58
|
+
|
|
59
|
+
## Observed output (PASS)
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
test_captcha_provider_dict (test_scraper.ApiKeyResolutionTests.test_captcha_provider_dict) ... ok
|
|
63
|
+
test_create_scraper_honors_captcha_dict (test_scraper.ApiKeyResolutionTests.test_create_scraper_honors_captcha_dict) ... ok
|
|
64
|
+
test_env_fallback (test_scraper.ApiKeyResolutionTests.test_env_fallback) ... ok
|
|
65
|
+
test_explicit_api_key (test_scraper.ApiKeyResolutionTests.test_explicit_api_key) ... ok
|
|
66
|
+
test_precedence_explicit_over_captcha_over_env (test_scraper.ApiKeyResolutionTests.test_precedence_explicit_over_captcha_over_env) ... ok
|
|
67
|
+
test_module_is_import_compatible (test_scraper.ConstructionTests.test_module_is_import_compatible) ... ok
|
|
68
|
+
test_returns_requests_session_subclass (test_scraper.ConstructionTests.test_returns_requests_session_subclass) ... ok
|
|
69
|
+
test_standard_session_api_present (test_scraper.ConstructionTests.test_standard_session_api_present) ... ok
|
|
70
|
+
test_detects_interstitial (test_scraper.DetectionTests.test_detects_interstitial) ... ok
|
|
71
|
+
test_detects_turnstile (test_scraper.DetectionTests.test_detects_turnstile) ... ok
|
|
72
|
+
test_extract_fields (test_scraper.DetectionTests.test_extract_fields) ... ok
|
|
73
|
+
test_extract_sitekey (test_scraper.DetectionTests.test_extract_sitekey) ... ok
|
|
74
|
+
test_ignores_real_page (test_scraper.DetectionTests.test_ignores_real_page) ... ok
|
|
75
|
+
test_cloudscraper_kwargs_accepted (test_scraper.DropInKwargsTests.test_cloudscraper_kwargs_accepted) ... ok
|
|
76
|
+
test_sess_state_adopted (test_scraper.DropInKwargsTests.test_sess_state_adopted) ... ok
|
|
77
|
+
test_turnstile_payload_proxy (test_scraper.PayloadTests.test_turnstile_payload_proxy) ... ok
|
|
78
|
+
test_turnstile_payload_shape (test_scraper.PayloadTests.test_turnstile_payload_shape) ... ok
|
|
79
|
+
test_interstitial_5s_sets_clearance_and_rerequests (test_scraper.RequestFlowTests.test_interstitial_5s_sets_clearance_and_rerequests) ... ok
|
|
80
|
+
test_max_solve_attempts_stops_loops (test_scraper.RequestFlowTests.test_max_solve_attempts_stops_loops) ... ok
|
|
81
|
+
test_non_challenge_passes_through (test_scraper.RequestFlowTests.test_non_challenge_passes_through) ... ok
|
|
82
|
+
test_proxy_forwarded_to_peak (test_scraper.RequestFlowTests.test_proxy_forwarded_to_peak) ... ok
|
|
83
|
+
test_session_proxy_used_when_not_explicit (test_scraper.RequestFlowTests.test_session_proxy_used_when_not_explicit) ... ok
|
|
84
|
+
test_turnstile_solve_and_retry (test_scraper.RequestFlowTests.test_turnstile_solve_and_retry) ... ok
|
|
85
|
+
|
|
86
|
+
----------------------------------------------------------------------
|
|
87
|
+
Ran 23 tests in 0.003s
|
|
88
|
+
|
|
89
|
+
OK
|
|
90
|
+
```
|
|
Binary file
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""cloudscraper-turnstile: a cloudscraper drop-in that solves Cloudflare
|
|
2
|
+
Turnstile and the 5s interstitial through the Peak API.
|
|
3
|
+
|
|
4
|
+
Swap two lines and existing code keeps working::
|
|
5
|
+
|
|
6
|
+
import cloudscraper_turnstile as cloudscraper
|
|
7
|
+
|
|
8
|
+
scraper = cloudscraper.create_scraper(api_key="pk_your_api_key")
|
|
9
|
+
r = scraper.get("https://protected.example.com/")
|
|
10
|
+
print(r.status_code, r.text)
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from .detect import (
|
|
16
|
+
extract_sitekey,
|
|
17
|
+
extract_turnstile_fields,
|
|
18
|
+
is_cloudflare_challenge,
|
|
19
|
+
is_interstitial_challenge,
|
|
20
|
+
is_turnstile_challenge,
|
|
21
|
+
)
|
|
22
|
+
from .peak import (
|
|
23
|
+
PeakClient,
|
|
24
|
+
PeakError,
|
|
25
|
+
build_cloudflare5s_payload,
|
|
26
|
+
build_turnstile_payload,
|
|
27
|
+
)
|
|
28
|
+
from .scraper import CloudScraper, create_scraper, resolve_api_key
|
|
29
|
+
|
|
30
|
+
__version__ = "0.1.0"
|
|
31
|
+
|
|
32
|
+
__all__ = [
|
|
33
|
+
"create_scraper",
|
|
34
|
+
"CloudScraper",
|
|
35
|
+
"resolve_api_key",
|
|
36
|
+
"PeakClient",
|
|
37
|
+
"PeakError",
|
|
38
|
+
"build_turnstile_payload",
|
|
39
|
+
"build_cloudflare5s_payload",
|
|
40
|
+
"is_cloudflare_challenge",
|
|
41
|
+
"is_turnstile_challenge",
|
|
42
|
+
"is_interstitial_challenge",
|
|
43
|
+
"extract_sitekey",
|
|
44
|
+
"extract_turnstile_fields",
|
|
45
|
+
"__version__",
|
|
46
|
+
]
|