Captcha solver for
Scrapy
Resolve the challenge inside the run instead of failing the job. A working Python example, and the three things that decide whether your token is accepted.
# middlewares.py
import time
import requests
CAPTCHANO_KEY = "cn-YOUR_API_KEY"
HEADERS = {"Authorization": f"Bearer {CAPTCHANO_KEY}"}
def solve(task: dict) -> dict:
"""Block until solved; returns e.g. {"token": "...", "userAgent": "..."}."""
r = requests.post(
"https://api.captchano.com/v1/solve",
params={"wait": 60},
json=task,
headers=HEADERS,
timeout=75,
)
data = r.json()
# A solve can outlast the 60 s wait: poll the task until it finishes.
while data.get("status") in ("queued", "solving"):
time.sleep(5)
data = requests.get(
f"https://api.captchano.com/v1/task/{data['id']}",
headers=HEADERS,
timeout=30,
).json()
if data.get("status") != "ready":
raise RuntimeError(data.get("error") or data)
return data["solution"]
class CaptchaNoMiddleware:
def process_response(self, request, response, spider):
sitekey = response.css("[data-sitekey]::attr(data-sitekey)").get()
if not sitekey:
return response
solution = solve({"type": "recaptcha_v2", "sitekey": sitekey, "url": response.url})
return request.replace(
method="POST",
body=f"g-recaptcha-response={solution['token']}",
headers={"Content-Type": "application/x-www-form-urlencoded"},
dont_filter=True,
)What decides whether the token is accepted
- Pass your proxy through. The token should be minted behind the same exit IP that submits it. For session-graded systems this is mandatory, not an optimisation.
- Match the user agent. Turnstile and several anti-bot systems bind the token to a user agent. We return the one used so you can replay it exactly.
- Request late. Tokens expire in roughly 120 seconds. Ask for one when you are ready to submit.
Blocking or polling
The example uses ?wait=60, so the call returns the finished solve and your Scrapy step stays linear. If you would rather not hold a connection, drop the parameter, keep the returned id, and poll GET /v1/task/{id} every five seconds. A token you have paid for stays retrievable for ten minutes, so a dropped connection does not cost you the solve.
Cost
$0.0045 per successful reCAPTCHA v2 solve, with failed solves never billed. See pricing for every challenge type, or the API reference for the full request shape.
How the solve fits into Scrapy
Scrapy has no browser, so the captcha is handled in a downloader middleware: detect the challenge in the response, solve, and retry the request with the token or cookie.
- 01Detect in process_response
Look for a data-sitekey or a known challenge page in the response body.
- 02Solve
Call the API with the sitekey, response.url and the proxy from request.meta.
- 03Retry with the token
Return a FormRequest carrying the token, or the original request with the solved cookie.
- 04Keep the cookie jar
Let Scrapy's cookie middleware keep the cleared session for the next requests.
Getting the token into the page
The middleware adds the token to the form data or sets the returned cookie, then returns a new Request.
Where integrations usually go wrong
A synchronous HTTP call in a middleware stalls every request. Use an async client or a thread for the solve.
If a proxy middleware rotates IPs per request, pin the IP for the retry so it matches the solve.
Cap retries per URL. A page that challenges every time is telling you to slow down.
Scrapy captcha FAQ
Can Scrapy solve JavaScript challenges?
Scrapy does not run JavaScript. For token challenges you only need the sitekey and URL; for session-graded systems like DataDome you send the challenge URL and receive a cookie that Scrapy can replay.
Should I use scrapy-playwright instead?
Only if the page needs JavaScript to render its content. For token-based captchas the middleware approach is lighter and faster than running a browser per request.
Where should the middleware sit?
After the proxy middleware and before the retry middleware, so it sees the proxy that will be used and its own retries are not double-counted.
How do I avoid paying for repeated solves?
Keep cookies between requests and pace the crawl. A cleared session often stays clear for many pages, and you are only billed for successful solves.
Guides that help
Ready to build?
Start with a free 7-day trial of the Hobby plan, then pay per successful solve or keep the plan as your traffic grows. Cancel before the trial ends and you pay nothing.