| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- import requests
- import time
- import json
- class CaptchaTester:
- def __init__(self, api_key):
- self.config = {
- 'capsolver_key': api_key
- }
- def _log(self, message):
- print(f"[{time.strftime('%H:%M:%S')}] {message}")
- def solve_captcha(self, page_url: str, task_type: str, site_key: str, use_proxy=False, action: str = None) -> str:
- """通用解决验证码"""
- capsolver_key = self.config.get('capsolver_key')
- if not capsolver_key:
- raise ValueError("Capsolver API key missing")
- task = {
- "type": task_type,
- "websiteURL": page_url,
- "websiteKey": site_key,
- }
-
- if action:
- task["pageAction"] = action
- payload = {"clientKey": capsolver_key, "task": task}
-
- # 创建任务
- res = requests.post("https://api.capsolver.com/createTask", json=payload, timeout=20)
- resp_json = res.json()
- if resp_json.get("errorId") != 0:
- raise Exception(f"创建任务失败: {res.text}")
- task_id = resp_json.get("taskId")
- self._log(f"任务已创建: {task_id}. 正在等待结果...")
- # 轮询结果
- for i in range(20):
- r = requests.post(
- "https://api.capsolver.com/getTaskResult",
- json={"clientKey": capsolver_key, "taskId": task_id},
- timeout=20
- )
- data = r.json()
- if data.get("status") == "ready":
- self._log("验证码解决成功!")
- return data["solution"].get("gRecaptchaResponse") or data["solution"].get("token")
-
- self._log(f"等待中... ({i+1}次)")
- time.sleep(3)
- raise Exception("Capsolver 任务超时")
- def test_score(self):
- # 1. 参数配置
- test_page = "https://antcpt.com"
- site_key = "6LcR_okUAAAAAPYrPe-HK_0RULO1aZM15ENyM-Mf"
- action = "homepage"
-
- try:
- # 2. 获取 Token
- token = self.solve_captcha(
- page_url=test_page,
- task_type="ReCaptchaV3TaskProxyLess", # 或者使用 ReCaptchaV3Task
- site_key=site_key,
- action=action
- )
- # 3. 发送到评分网站
- self._log("正在提交 Token 到 ar1n.xyz 进行评分...")
- score_url = 'https://ar1n.xyz/recaptcha3ScoreTest'
- headers = {
- 'Accept': 'application/json, text/javascript, */*; q=0.01',
- 'Content-Type': 'application/json; charset=UTF-8',
- 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36',
- 'Origin': 'https://antcpt.com',
- 'Referer': 'https://antcpt.com/'
- }
- # 注意:这里的 key 必须是 g-recaptcha-reponse (网站特定的拼写错误)
- post_data = {"g-recaptcha-reponse": token}
-
- response = requests.post(score_url, headers=headers, json=post_data)
-
- if response.status_code == 200:
- result = response.json()
- print("\n" + "="*30)
- print("【评分结果】")
- print(json.dumps(result, indent=4))
- print("="*30)
- else:
- print(f"评分请求失败: {response.status_code}, {response.text}")
- except Exception as e:
- print(f"发生错误: {e}")
- # --- 运行测试 ---
- if __name__ == "__main__":
- MY_CAPSOLVER_KEY = "CAP-5441DD341DD3CC2FAEF0BE6FE493EE9A"
- tester = CaptchaTester(MY_CAPSOLVER_KEY)
- tester.test_score()
|