此页内容

【Python】SDK

captchaRun

188字小于1分钟

2024-05-22

Python:

captchaRun

pip3 install captcharun

Python调用代码:

import time
from captcharun import Client, GetTask, CreateTask
from captcharun.task import ReCaptchaV2Task
import requests


# 你可以手动指定 token
client = Client("xxxxx")
# 不然会自动从环境变量 CAPTCHARUN_TOKEN 获取 
# client = Client()


def get_token(timeout = 180):
    result = client.invoke(
        CreateTask(
            ReCaptchaV2Task(
            "6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-",
            "https://www.google.com/recaptcha/api2/demo"
            ),
            # developer="开发者 ID"
        ),
    )

    task_id = result.get("taskId")
    if task_id is None:
        print("创建任务失败")
        return
    
    start_time = time.time()
    result = client.invoke(GetTask(task_id))

    while result["status"] == "Working" and time.time() - start_time < timeout:
        time.sleep(3)
        result = client.invoke(GetTask(task_id))
    
    return result['response']['gRecaptchaResponse']


def get_token_with_retry(retry_times=3, timeout=180):
    for _ in range(retry_times):
        try:
            token = get_token(timeout)
            if token is not None:
                return token
        except Exception as e:
            print(e)

    return None

def verify_website(response):
    url = "https://www.google.com/recaptcha/api2/demo"
    data = {"g-recaptcha-response": response}
    r = requests.post(url, data=data)
    if r.status_code == 200:
        return r.text
        


if __name__ == '__main__':
    token = get_token_with_retry()
    print(token)
    result = verify_website(token)
    print(result)