test_capsolver.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import requests
  2. import time
  3. import json
  4. class CaptchaTester:
  5. def __init__(self, api_key):
  6. self.config = {
  7. 'capsolver_key': api_key
  8. }
  9. def _log(self, message):
  10. print(f"[{time.strftime('%H:%M:%S')}] {message}")
  11. def solve_captcha(self, page_url: str, task_type: str, site_key: str, use_proxy=False, action: str = None) -> str:
  12. """通用解决验证码"""
  13. capsolver_key = self.config.get('capsolver_key')
  14. if not capsolver_key:
  15. raise ValueError("Capsolver API key missing")
  16. task = {
  17. "type": task_type,
  18. "websiteURL": page_url,
  19. "websiteKey": site_key,
  20. }
  21. if action:
  22. task["pageAction"] = action
  23. payload = {"clientKey": capsolver_key, "task": task}
  24. # 创建任务
  25. res = requests.post("https://api.capsolver.com/createTask", json=payload, timeout=20)
  26. resp_json = res.json()
  27. if resp_json.get("errorId") != 0:
  28. raise Exception(f"创建任务失败: {res.text}")
  29. task_id = resp_json.get("taskId")
  30. self._log(f"任务已创建: {task_id}. 正在等待结果...")
  31. # 轮询结果
  32. for i in range(20):
  33. r = requests.post(
  34. "https://api.capsolver.com/getTaskResult",
  35. json={"clientKey": capsolver_key, "taskId": task_id},
  36. timeout=20
  37. )
  38. data = r.json()
  39. if data.get("status") == "ready":
  40. self._log("验证码解决成功!")
  41. return data["solution"].get("gRecaptchaResponse") or data["solution"].get("token")
  42. self._log(f"等待中... ({i+1}次)")
  43. time.sleep(3)
  44. raise Exception("Capsolver 任务超时")
  45. def test_score(self):
  46. # 1. 参数配置
  47. test_page = "https://antcpt.com"
  48. site_key = "6LcR_okUAAAAAPYrPe-HK_0RULO1aZM15ENyM-Mf"
  49. action = "homepage"
  50. try:
  51. # 2. 获取 Token
  52. token = self.solve_captcha(
  53. page_url=test_page,
  54. task_type="ReCaptchaV3TaskProxyLess", # 或者使用 ReCaptchaV3Task
  55. site_key=site_key,
  56. action=action
  57. )
  58. # 3. 发送到评分网站
  59. self._log("正在提交 Token 到 ar1n.xyz 进行评分...")
  60. score_url = 'https://ar1n.xyz/recaptcha3ScoreTest'
  61. headers = {
  62. 'Accept': 'application/json, text/javascript, */*; q=0.01',
  63. 'Content-Type': 'application/json; charset=UTF-8',
  64. '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',
  65. 'Origin': 'https://antcpt.com',
  66. 'Referer': 'https://antcpt.com/'
  67. }
  68. # 注意:这里的 key 必须是 g-recaptcha-reponse (网站特定的拼写错误)
  69. post_data = {"g-recaptcha-reponse": token}
  70. response = requests.post(score_url, headers=headers, json=post_data)
  71. if response.status_code == 200:
  72. result = response.json()
  73. print("\n" + "="*30)
  74. print("【评分结果】")
  75. print(json.dumps(result, indent=4))
  76. print("="*30)
  77. else:
  78. print(f"评分请求失败: {response.status_code}, {response.text}")
  79. except Exception as e:
  80. print(f"发生错误: {e}")
  81. # --- 运行测试 ---
  82. if __name__ == "__main__":
  83. MY_CAPSOLVER_KEY = "CAP-5441DD341DD3CC2FAEF0BE6FE493EE9A"
  84. tester = CaptchaTester(MY_CAPSOLVER_KEY)
  85. tester.test_score()