from dataclasses import dataclass from typing import Dict, Optional from pynocaptcha import HcaptchaCracker from loguru import logger @dataclass class PynoCaptchaConfig: user_token: str sitekey: str = "8cf23430-f9c8-4aaa-9ba2-da32f65adf2e" # 默认值 referer: str = "https://store.steampowered.com/join/" timeout: int = 60 debug: bool = True user_agent: str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; Valve Steam Client/default/1741737356) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.6478.183 Safari/537.36" class PynoCaptchaSolver: def __init__(self, config: PynoCaptchaConfig): self.config = config logger.debug(f"PynoCaptchaSolver 初始化 - 使用 PyNoCaptcha 库") def solve_hcaptcha(self) -> Optional[Dict]: """ 使用 PyNoCaptcha 库解决 HCaptcha 验证码 返回: 成功时返回包含验证结果的字典,失败时返回None """ try: logger.debug(f"开始解决验证码 - sitekey: {self.config.sitekey}") logger.debug(f"使用User-Agent: {self.config.user_agent}") # 创建验证码破解器 cracker = HcaptchaCracker( user_token=self.config.user_token, sitekey=self.config.sitekey, referer=self.config.referer, user_agent=self.config.user_agent, debug=self.config.debug, timeout=self.config.timeout, ) # 破解验证码 logger.debug("调用 PyNoCaptcha 进行破解") result = cracker.crack() if result: logger.debug("验证码破解成功") return { "captcha_text": result.get("generated_pass_UUID"), "token": result.get("generated_pass_UUID"), "solution": result } else: logger.error("验证码破解失败") return None except Exception as e: logger.error(f"验证码破解过程中出错: {str(e)}") return None # 使用示例 if __name__ == "__main__": # 配置 config = PynoCaptchaConfig( user_token="cf169d36-0d62-45da-bff7-1eff42bbc4f3", sitekey="8cf23430-f9c8-4aaa-9ba2-da32f65adf2e", referer="https://store.steampowered.com/join/", user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; Valve Steam Client/default/1741737356) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.6478.183 Safari/537.36" ) # 初始化求解器 solver = PynoCaptchaSolver(config) # 解决验证码 result = solver.solve_hcaptcha() if result: print(f"验证成功: {result['captcha_text'][:30]}...") else: print("验证失败")