Join And Get Free Trial!

如何使用 Python代码示例绕过 reCaptcha v3#

logoNextCaptcha
August 01,2024

概述#

自动化网站交互可以大大简化测试和重复性任务。然而,一个经常出现的问题是 reCAPTCHA 识别,其目的是区分人类用户和机器人。本指南将向您展示如何使用 Python 和NextCaptcha服务解决 reCAPTCHA v3 挑战,该服务使用人工解算器以合乎道德和合法的方式处理这些 CAPTCHA。

准备#

开始之前,您需要:
  • 注册NextCaptcha账户: 前往 NextCaptcha并注册以获取你的API密钥。

  • 安装必要的 Python 包: 确保您已 requests 安装。如果你还没有安装,可以使用 pip 安装:

      pip install requests

查找 Sitekey#

要解决 reCAPTCHA,您需要站点密钥,该密钥嵌入在使用 reCAPTCHA 的页面的 HTML 中。您可以按照以下方法找到它:
  • 使用 reCAPTCHA 导航至页面.

  • 检查页面的源代码: 右键单击页面并选择“检查”或“查看页面源代码”。

  • 搜索站点密钥: 寻找 data-sitekey HTML 中的属性。它看起来会像这样:

      <div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>

    复制此 YOUR_SITE_KEY 价值.

解决验证验证码#

现在您有了站点密钥,您可以使用 NextCaptcha API 来解决 reCAPTCHA 挑战。
  • 向NextCaptcha发送请求以解决 reCAPTCHA v3:

    import requests
     
    # Your NextCaptcha API key
    API_KEY = 'YOUR_NextCAPTCHA_API_KEY'
     
    # The URL of the page with the reCAPTCHA
    url = 'URL_OF_THE_PAGE_WITH_RECAPTCHA'
     
    # The site key for reCAPTCHA v3
    site_key = 'YOUR_SITE_KEY'
     
    # Requesting the reCAPTCHA solution
    response = requests.post('https://api.nextcaptcha.com/createTask', data={
        "clientKey": API_KEY,
        "task": {
            "type":"RecaptchaV3TaskProxyless",
            "websiteURL":url,
            "websiteKey":site_key
        }
    })
     
    request_id = response.json().get('taskId')
     

  • 投票寻找解决方案:

    import time
     
    solution = None
    while solution is None:
        time.sleep(1)  # Wait a few seconds before checking again
        response = requests.post('https://api.nextcaptcha.com/getTaskResult', data={
    		  "clientKey": "api key",
    		  "taskId": request_id
    		})
        if response.json().get('errorId') == 0:
            solution = response.json().get('solution')
     
    # The reCAPTCHA token
    recaptcha_token = solution["gRecaptchaResponse"]
    print(f'reCAPTCHA token: {recaptcha_token}')
     

提交已解决的验证码#

获取 reCAPTCHA 令牌后,您可以将其与必须使用验证码解决方案的表单或请求一起提交。以下是提交表单的示例:
# URL where the form is submitted
form_submit_url = 'URL_TO_SUBMIT_FORM'
 
# Form data including the reCAPTCHA token
form_data = {
    'some_form_field': 'value',
    'g-recaptcha-response': recaptcha_token
}
 
# Submit the form
response = requests.post(form_submit_url, data=form_data)
print(response.text)
 

自动解决 reCaptcha 的完整代码#

综合以上所有步骤,以下是完整代码:

import requests
import time
 
# Your NextCaptcha API key
API_KEY = 'YOUR_NextCAPTCHA_API_KEY'
 
# The URL of the page with the reCAPTCHA
url = 'URL_OF_THE_PAGE_WITH_RECAPTCHA'
 
# The site key for reCAPTCHA v3
site_key = 'YOUR_SITE_KEY'
 
# Requesting the reCAPTCHA solution
response = requests.post('https://api.nextcaptcha.com/createTask', data={
    "clientKey": API_KEY,
    "task": {
        "type":"RecaptchaV3TaskProxyless",
        "websiteURL":url,
        "websiteKey":site_key
    }
})
 
task_id = response.json().get('taskId')
 
solution = None
while solution is None:
    time.sleep(1)  # Wait a few seconds before checking again
    response = requests.post('https://api.nextcaptcha.com/getTaskResult', data={
		  "clientKey": "api key",
		  "taskId": task_id
		})
    if response.json().get('errorId') == 0:
        solution = response.json().get('solution')
 
# The reCAPTCHA token
recaptcha_token = solution["gRecaptchaResponse"]
print(f'reCAPTCHA token: {recaptcha_token}')
 
# URL where the form is submitted
form_submit_url = 'URL_TO_SUBMIT_FORM'
 
# Form data including the reCAPTCHA token
form_data = {
    'some_form_field': 'value',
    'g-recaptcha-response': recaptcha_token
}
 
# Submit the form
response = requests.post(form_submit_url, data=form_data)
print(response.text)
通过遵循本指南,您可以以尊重与您交互的网站的服务条款的方式自动解决 reCAPTCHA v3 挑战。