More languages
How to solve GeeTest in Python
GeeTest comes in two generations with different inputs and outputs. This guide models both as small dataclasses, solves them with an async httpx client, and builds the site verification request from the result. Each solve costs $0.84 per 1,000.
Two versions, one task type
Both GeeTest generations use the task type GeeTestTaskProxyLess. What changes is the input and the output:
| GeeTest v3 | GeeTest v4 | |
|---|---|---|
| Inputs | gt and a fresh challenge | captchaId |
| Where they come from | A register request the site makes before showing the puzzle | The page script, constant per site |
| Result | challenge plus validate and seccode | five values: captcha_id, lot_number, pass_token, gen_time and captcha_output |
If you are not sure which version a page uses, look at the scripts: v3 loads gt.js and calls initGeetest, v4 loads gt4.js and calls initGeetest4. The GeeTest entry has more on the puzzle types. Our GeeTest v3 and v4 overview covers the version differences at length; this guide focuses on an async Python client.
Model the inputs as dataclasses
Separate classes make it impossible to send a v3 task without a challenge or a v4 task without a captcha id, and each class knows how to turn itself into a task.
from dataclasses import dataclass
@dataclass(frozen=True)
class GeeV3:
page_url: str
gt: str
challenge: str # must be fresh for every solve
def as_task(self) -> dict:
return {"type": "GeeTestTaskProxyLess", "websiteURL": self.page_url,
"gt": self.gt, "challenge": self.challenge}
@dataclass(frozen=True)
class GeeV4:
page_url: str
captcha_id: str
def as_task(self) -> dict:
return {"type": "GeeTestTaskProxyLess", "websiteURL": self.page_url,
"captchaId": self.captcha_id}
An async solver with httpx
An httpx.AsyncClient shares connections across many solves, which is useful when a job handles several GeeTest pages at the same time. Add it to your environment with pip install httpx.
import asyncio
import os
import httpx
class GeeSolveError(RuntimeError):
pass
async def solve_gee(http: httpx.AsyncClient, puzzle: GeeV3 | GeeV4) -> dict:
key = os.environ.get("GATESOLVER_KEY", "YOUR_API_KEY")
async def rpc(route: str, **fields) -> dict:
resp = await http.post(f"https://gatesolver.com/{route}", json={"clientKey": key, **fields})
data = resp.json()
if data.get("errorId"):
raise GeeSolveError(f'{route} {data.get("errorCode")}: {data.get("errorDescription", "")}')
return data
task_id = (await rpc("createTask", task=puzzle.as_task()))["taskId"]
async with asyncio.timeout(125):
while True:
await asyncio.sleep(3)
progress = await rpc("getTaskResult", taskId=task_id)
if progress["status"] == "ready":
return progress["solution"]
if progress["status"] == "failed":
raise GeeSolveError("GeeTest task failed; it was not charged")
asyncio.timeout requires Python 3.11 or newer; on 3.10, wrap the loop in asyncio.wait_for instead. The 125 second ceiling matches the API's own limit: a task unsolved after 120 seconds is failed and free.
v3: get a fresh challenge, then post validate and seccode
For v3 the site issues a new challenge every time the puzzle is about to appear, usually from an endpoint like /captcha/register that returns gt and challenge as JSON. Find it in the Network tab. Your code must call it right before each solve, because a challenge validates once at most.
async def v3_login(username: str, password: str) -> int:
async with httpx.AsyncClient(timeout=25) as http:
site = "https://members.example.net"
reg = (await http.get(f"{site}/captcha/register", params={"t": "login"})).json()
sol = await solve_gee(http, GeeV3(f"{site}/login", reg["gt"], reg["challenge"]))
res = await http.post(f"{site}/api/login", data={
"username": username, "password": password,
"geetest_challenge": sol["challenge"],
"geetest_validate": sol["validate"],
"geetest_seccode": sol["seccode"],
})
return res.status_code
The field names geetest_challenge, geetest_validate and geetest_seccode are the ones the standard GeeTest v3 client posts, and most sites keep them. Confirm in the Network tab, since some sites rename them or wrap them in JSON. Use the challenge from the solution rather than the one you sent; it may differ.
v4: the captcha id and five result fields
For v4 the captchaId is a 32 character hex string in the page, typically in the initGeetest4({captchaId: ...}) call or in the URL of the load request the script makes. It does not change between visits, so you can store it in configuration.
V4_FIELDS = ("captcha_id", "lot_number", "pass_token", "gen_time", "captcha_output")
async def v4_vote(poll_id: str) -> dict:
async with httpx.AsyncClient(timeout=25) as http:
page = "https://events.example.org/poll/" + poll_id
sol = await solve_gee(http, GeeV4(page, "e392e1d7fd421dc63325744d5a2b9c73"))
proof = {name: sol[name] for name in V4_FIELDS}
res = await http.post("https://events.example.org/api/vote",
json={"pollId": poll_id, "choice": "b", **proof})
return res.json()
Pass the five values exactly as returned. They are verified together by the site's server, so changing or dropping one makes the whole proof invalid.
Solving several pages at once
Because the solver is async, a batch is a gather with a semaphore. The API has no limit on concurrent tasks, but each key allows 600 requests per minute, and a running solve polls about twenty times a minute.
async def many_v4(pages: list[str], parallel: int = 15) -> list:
gate = asyncio.Semaphore(parallel)
async with httpx.AsyncClient(timeout=25) as http:
async def one(url: str):
async with gate:
return await solve_gee(http, GeeV4(url, "e392e1d7fd421dc63325744d5a2b9c73"))
return await asyncio.gather(*(one(u) for u in pages), return_exceptions=True)
Common problems
- v3 validates fail every time. The challenge was reused or fetched too early. Register, solve and post in one quick sequence.
- ERROR_INVALID_TASK_DATA. A v3 task without
gtorchallenge, or a v4 task withoutcaptchaId. The error description says which one. - Site says the captcha expired. Too much time passed between solve and post. Results are short lived; use them immediately.
- v4 accepted by the API, refused by the site. A field was renamed or missing. Compare your request with the browser's.
Typical solve time is under 5 seconds. See GeeTest v3 / v4 for every field.
Finding the register endpoint for v3
For v3, the hardest part is often finding where the fresh challenge comes from. Open the Network tab, clear it, and trigger the puzzle (often by clicking the login button). Look for a request made just before the GeeTest scripts load, returning JSON with gt, challenge and usually success and new_captcha. That is the register endpoint. It may carry a timestamp parameter, a session cookie, or both, so call it from the same AsyncClient that will later post the login, which keeps cookies consistent.
If the response has success: 0, the site is in GeeTest's fallback mode and may not need a real solve; check how the page behaves in that case before sending a task.
Keeping the client alive
Creating an AsyncClient per solve is fine for scripts. In a long running service, create one at start up and pass it in, so connections to the API are pooled. The httpx guide covers client lifetimes, timeouts and HTTP/2 options in more depth.
Retrying
Wrap solve_gee in a function that retries once on GeeSolveError messages containing ERROR_CAPTCHA_UNSOLVABLE or a failed status. For v3, a retry must also fetch a new challenge first.
Typing the solution
If you prefer typed results end to end, add two more frozen dataclasses for the v3 and v4 solutions and build them from the returned dictionary with GeeV4Result(**{k: sol[k] for k in V4_FIELDS}). A missing field then fails loudly at that line instead of producing a silent bad request to the site.
Common questions
How do I tell GeeTest v3 from v4?
v3 pages load gt.js and call initGeetest with gt and challenge; v4 pages load gt4.js and call initGeetest4 with a captchaId.
Can I reuse a v3 challenge?
No. Each challenge validates once at most, so request a fresh one from the site right before every solve.
Does the v4 captchaId change?
No. It is fixed per site or per form, so you can keep it in configuration.
Which Python version does the code need?
Python 3.11 for asyncio.timeout and the union syntax in type hints. On 3.10, use asyncio.wait_for.
Is a failed GeeTest task charged?
No. Failed, unsolvable and timed out tasks are free; only returned solutions are charged.
Keep reading
- GeeTest v3 / v4 solver$0.84 per 1,000 tasks, answers under 5 seconds.
- Solve GeeTest v3 and v4Tell GeeTest v3 from v4, collect gt and a fresh challenge or the captcha_id, then pass every returned field on.
- GeeTestGeeTest v3 and v4 slide and click puzzles, the gt, challenge and captcha_id inputs, and the values a solve hands back.
- Scraping with HTTPXHTTPX for scrapers: HTTP/2 on one connection, timeouts split by phase, connection limits, event hooks, and the same code sync or async.