绕过 reCAPTCHA V2/V3:Python 与 Selenium 实践指南

前言

验证码(CAPTCHA)技术已经存在许多年,尽管它的有效性一直备受争议,但许多网站仍然依赖它来保护资源。尤其是 Google 推出的 reCAPTCHA 系列,一直是验证码领域的佼佼者。本文将详细介绍如何绕过 reCAPTCHA V2 和 V3,并提供实用的代码示例。

详情请见:解决验证码recaptcha、cloudflare、incapsula


1. 什么是 reCAPTCHA?

reCAPTCHA 是 Google 推出的一种验证码技术,旨在通过分析用户行为来区分人类和机器人。它目前有三个主要版本:

  • reCAPTCHA V1:传统的扭曲文本验证码(已停用)。
  • reCAPTCHA V2:用户需要点击复选框或完成图片任务。
  • reCAPTCHA V3:无感验证,通过分析用户行为生成风险评分。

  • 2. 绕过 reCAPTCHA 的方法

    2.1 绕过 reCAPTCHA V2
    reCAPTCHA V2 的工作原理

    reCAPTCHA V2 主要通过以下两种方式验证用户身份:

    1. 复选框验证:用户点击“我不是机器人”按钮即可通过验证。
    2. 图片任务:用户需要从图片中选择特定对象(如交通灯、斑马线等)。

    尽管 reCAPTCHA V2 提升了用户体验,但它仍然可以被一些自动化工具绕过。

    绕过方法

    以下是一些常见的绕过方法:

    1. 使用 nocaptcha 服务

    nocaptcha 是一种流行的验证码解决服务,支持多种编程语言。以下是 Python 示例代码:

    import requests
    import time
    
    API_KEY = 'Your_API_2Captcha_key'
    
    def solve_recaptcha_v2(site_key, url):
        payload = {
            'key': API_KEY,
            'method': 'userrecaptcha',
            'googlekey': site_key,
            'pageurl': url,
            'json': 1
        }
        response = requests.post('https://.com/in.php', data=payload)
        result = response.json()
        
        if result['status'] != 1:
            raise Exception(f"Error when sending captcha: {result['request']}")
        
        captcha_id = result['request']
        while True:
            time.sleep(5)
            response = requests.get(f"https://.com/res.php?key={API_KEY}&action=get&id={captcha_id}&json=1")
            result = response.json()
            if result['status'] == 1:
                return result['request']
            elif result['request'] == 'CAPCHA_NOT_READY':
                continue
            else:
                raise Exception(f"Error while solving captcha: {result['request']}")
    
    site_key = 'your_site_key_v2'
    url = 'https://example.com'
    token = solve_recaptcha_v2(site_key, url)
    print(f"Received token: {token}")
    
    1. 使用 Selenium 自动化

    Selenium 是一个强大的浏览器自动化工具,可模拟用户行为以完成验证。


    2.2 绕过 reCAPTCHA V3
    reCAPTCHA V3 的工作原理

    与 V2 不同,reCAPTCHA V3 是无感验证。系统会根据用户行为生成 0.1 到 1.0 的风险评分,分数越高,用户越可能是真人。

    绕过方法

    以下是绕过 reCAPTCHA V3 的一些方法:

    1. 使用验证码解决服务

    支持 reCAPTCHA V3 的服务也可以使用。以下是 Python 示例代码:

    def solve_recaptcha_v3(site_key, url, action='verify', min_score=0.3):
        payload = {
            'key': API_KEY,
            'method': 'userrecaptcha',
            'googlekey': site_key,
            'pageurl': url,
            'version': 'v3',
            'action': action,
            'min_score': min_score,
            'json': 1
        }
        response = requests.post('https://2captcha.com/in.php', data=payload)
        result = response.json()
        
        if result['status'] != 1:
            raise Exception(f"Error when sending captcha: {result['request']}")
        
        captcha_id = result['request']
        while True:
            time.sleep(5)
            response = requests.get(f"https://2captcha.com/res.php?key={API_KEY}&action=get&id={captcha_id}&json=1")
            result = response.json()
            if result['status'] == 1:
                return result['request']
            elif result['request'] == 'CAPCHA_NOT_READY':
                continue
            else:
                raise Exception(f"Error while solving captcha: {result['request']}")
    
    site_key = 'your_site_key_v3'
    url = 'https://example.com'
    token = solve_recaptcha_v3(site_key, url)
    print(f"Received token: {token}")
    
    1. 模拟用户行为

    通过 Selenium 或 Puppeteer 模拟用户行为,避免触发高风险评分。

    详情请见:解决验证码recaptcha、cloudflare、incapsula


    3. 其他语言的实现

    JavaScript

    在 Node.js 中,可以使用以下模块:

  • 2captcha:官方模块,支持 TypeScript。
  • captcha-solver:集成 Puppeteer,提供开箱即用的解决方案。
  • 以下是 Node.js 示例代码:

    const axios = require('axios');
    const sleep = require('util').promisify(setTimeout);
    
    const API_KEY = 'YOUR_API_KEY_2CAPTCHA';
    
    async function solveReCaptchaV2(siteKey, pageUrl) {
        const sendCaptchaResponse = await axios.post('http://2captcha.com/in.php', null, {
            params: {
                key: API_KEY,
                method: 'userrecaptcha',
                googlekey: siteKey,
                pageurl: pageUrl,
                json: 1
            }
        });
        const requestId = sendCaptchaResponse.data.request;
    
        while (true) {
            await sleep(5000);
            const getResultResponse = await axios.get('http://2captcha.com/res.php', {
                params: {
                    key: API_KEY,
                    action: 'get',
                    id: requestId,
                    json: 1
                }
            });
    
            if (getResultResponse.data.status === 1) {
                return getResultResponse.data.request;
            } else if (getResultResponse.data.request === 'CAPCHA_NOT_READY') {
                continue;
            } else {
                throw new Error(`Error while solving captcha: ${getResultResponse.data.request}`);
            }
        }
    }
    
    (async () => {
        const siteKey = 'YOUR_SITE_KEY';
        const pageUrl = 'https://example.com';
        const token = await solveReCaptchaV2(siteKey, pageUrl);
        console.log(`Received token: ${token}`);
    })();
    

    详情请见:解决验证码recaptcha、cloudflare、incapsula

    作者:qq_33253945

    物联沃分享整理
    物联沃-IOTWORD物联网 » 绕过 reCAPTCHA V2/V3:Python 与 Selenium 实践指南

    发表回复