Python DEMO:Simulating submission via Selenium demo_selenium.py

这里只是演示简单的处理,不同的网站需要针对性的提交,参考这个解决思路即可,不要生搬硬套。

这是基于谷歌官方的演示,谷歌官方是reCaptcha V2:https://www.google.com/recaptcha/api2/demo

 

DEMO文件下载:

执行输出 :

创建任务: ea2d4234-2130-11ec-a50a-0242ac110003 {'errorId': 0, 'errorCode': '', 'errorDescription': None, 'status': 'processing', 'solution': None} {'errorId': 0, 'errorCode': '', 'errorDescription': '', 'status': 'processing', 'lock': True} {'errorId': 0, 'errorCode': '', 'errorDescription': '', 'status': 'processing', 'lock': True} 识别结果: 03AGdBq25wDNKt75stKICGOE1nWPgl-r0D2FEyjpDQ36CSzCiXfuA3AKwwK2gPKeYkHMer4YEcwcXn-1lYNWHIplL1JAxxp-yQAHbTN0E8az4wCWaSDBU79xrbsFAniREH0_7wVnrlHt7EI57nAxe546--A7zMH9n6h1F7BFLoKwcu_KSVUuM_wg8O9QwOSsBse3MgpfTOzsmd2r7wcnGhUkYl6hz_i7kt1U_7FuGQsWLUc8shYFHjiEYRTsaDuCTsAB3P7C8jgm9r-Mq5Xx6NmSNSJaacOmVspofEO4YJU_pKvIxKYkPVyY53hCql51lfCdA3DIZfVPUJdqdmV8iRIxsYnvn-3pA9v7272yMG3UfmpMFIn_KyIk7SGO-Pqjoj6CnMn_26AzMYXuy1712bpTVPCmYFddz0qV1UN8xIOK7wa_5a0FkW1gL1x_pBUAnzn1GdnjiBBOrGU0od7zzuEE7cdWi_QgiMSm09mLyfdvsNjg21eKJhNqN-b49Ay0Z9ylkvPjcqhknTWM5DAZMw8NSUH3YL0-vJyZYLMl7xBDvNssriG1OPa8v3lBguBWCxx9S8I4_qzQ9Tx4YQbeH8HCrBrnyTinThvH6lSWTUgS0p3S9BdqdfS12vPrLbFUq6U2rrsHK9tppXMKxLcu6_XQoURoZ922gvyfegnR8ARbuKsByvPxb4MJso9E0Pz3qh0nR3DvSQw29yhXTYe496CAqCGilzXwWWV3Dxv2OOOFW1IDePcArdTr07zDCpcOKJrJj6eiOrunVA6BVUWNcl0_ap9eJ_H1hn2RnQM-vA-9WDmq8ip0a41ziNt7r_LJfF-ZKmOkcAE2uK7mXz6L7yNgEtVHFu2JmUwioS9QLYitF5RzUdMuwfP7OuAqLxOd9nuxrHnLWQrrPbM4Pn3EhgUGU6if_idwMvJfXWoTNLBjdk1cngZ6Z-H4YGgVl_-c0yd7yGaNdEK41iuTkNB3jd7tT-i6GmiIDT6fpnplf_4ikngwodRy-2n0809gzyvrxRY-XLSsSnrVYIWpVmIDMqrzS8K1WpnDQNXA7VCY8kvwdSkcF9crxH2a1iQKsX2X7qJOePKUfspurWn08FtZNXcbDoj7lphEN0yWtur_L4P18uqi1-4uLbqMG_0osqx_QQlG2_o4bPBIIohFyr7CfbjWwkwY5Vvl_ijldNHGv6V0SrkjYklv4MGLkaqfVwzKfpKd8fZoXMtVCvmXB3tygwktomB5dcTxituwIYfPFRswwdSx_OFFc_r35PCy0Fj1EfKdjHZflOc3i35tb4imFgFmzhEGGxM-hH8rgmOzhaWVWHL7caBFy9py6eRQDM3cjIEled68u92r1DlavcQScjBaVbI0U4h7yVPTiD00fYluPDM9jtMVSXT9dFsmWlahLx-eMi-sqYHoFFn-djvozA_VPXCTQrGurIVXradfpL6oWT_lJl-2-XmtSfyGjp6sHqUw0_-USpxyI7fhVN9PYzxJBlDNg7qcDOCFLkySI2GAopCcln3_glta4q-O5jyRRKWMRENv34FmwQhIcp7qXugkDB-cR9AGCBjoxZ0UrU8XmcuspRpw7q03MBR5N6RTvYZRAMqrpt_lpysN5oBWCymGo-3Z3R9LMpUA 验证结果: <html dir="ltr"><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, user-scalable=yes"><title>ReCAPTCHA 演示</title><link rel="stylesheet" href="https://www.gstatic.com/recaptcha/releases/-TriQeni1Ls-Mdq_ssN2cUL5/demo__ltr.css" type="text/css"></head><body><div class="recaptcha-success">验证成功... 太棒了!</div></body></html>

 

DEMO源码:

import time import requests from selenium import webdriver """ YESCAPTCHA验证码DEMO selenium版本 目标网站 https://www.google.com/recaptcha/api2/demo 这是谷歌官方的演示 谷歌官方是reCaptcha V2 这里只是演示简单的处理 不同的网站需要针对性的提交 参考这个思路即可 不要生搬硬套 """ # clientKey:在个人中心获取 clientKey = "cc9c18d3e263515c2c072b36a7125eecc078618f3" # 目标参数: websiteKey = "6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-" # 目标参数: websiteURL = "https://www.google.com/recaptcha/api2/demo" # 验证码类型: task_type = "NoCaptchaTaskProxyless" def create_task() -> str: """ 第一步,创建验证码任务 :param :return taskId : string 创建成功的任务ID """ url = "https://api.yescaptcha.com/createTask" data = { "clientKey": clientKey, "task": { "websiteURL": websiteURL, "websiteKey": websiteKey, "type": task_type } } try: # 发送JSON格式的数据 result = requests.post(url, json=data, verify=False).json() taskId = result.get('taskId') if taskId is not None: return taskId print(result) except Exception as e: print(e) def get_response(taskID: str): """ 第二步:使用taskId获取response :param taskID: string :return response: string 识别结果 """ # 循环请求识别结果,3秒请求一次 times = 0 while times < 120: try: url = f"https://api.yescaptcha.com/getTaskResult" data = { "clientKey": clientKey, "taskId": taskID } result = requests.post(url, json=data, verify=False).json() solution = result.get('solution', {}) if solution: response = solution.get('gRecaptchaResponse') if response: return response print(result) except Exception as e: print(e) times += 3 time.sleep(3) def verify_website(response): """ 第三步:提交给网站进行验证 :param response: string :return html: string """ # 如果报错:Message: 'geckodriver' executable needs to be in PATH # 参考解决:https://www.jianshu.com/p/1d177b266fd2 # driver = webdriver.Firefox(executable_path=r'C:\Program Files\Mozilla Firefox\geckodriver.exe') driver = webdriver.Firefox() driver.get("https://www.google.com/recaptcha/api2/demo") # 每个网站的处理方式不同,但是大概思路是一样的 # 无外乎拿到验证码识别结果,然后想办法提交 # JS回调就是提交的一种 # 以下步骤请先看看官方官网的代码, # 理解一下三个步骤 # 在网页上执行JS,将获得的验证码写入网页 driver.execute_script(f'document.getElementById("g-recaptcha-response").value="{response}"') # 执行回调函数,每个网站回调函数并不相同,需要自己找一下 # 一般为data-callback=xxxx,这个xxxx就是回调函数 driver.execute_script(f'onSuccess("{response}")') # 点击提交 driver.find_element_by_id("recaptcha-demo-submit").click() return driver.page_source if __name__ == '__main__': taskId = create_task() print('创建任务:', taskId) if taskId is not None: response = get_response(taskId) print('识别结果:', response) result = verify_website(response) print('验证结果:', result)