绕过 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 示例代码:

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 示例代码:

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 示例代码:

javascript 复制代码
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

相关推荐
浩瀚地学13 小时前
deepagents学习打卡day08
经验分享·笔记·python·学习·agent
u01110267513 小时前
图片预览越来越占内存聊聊 URL.createObjectURL 的释放时机
图像处理·深度学习·算法·ai作画
Zane199413 小时前
同样是遍历图,为什么BFS能保证找到最短路径,DFS却不行
算法
无限码力13 小时前
2026华为OD机试真题 新系统 9月27日【坚果巧克力】
算法·华为od·华为od机考·华为od机试真题·华为od机试·华为od机考真题题解·华为社招笔试真题
samforce13 小时前
叹息之墙——人类认知 AI,终将遇到的一堵同构之墙
人工智能·深度学习·算法·哲学·认知科学
卷无止境13 小时前
便宜模型和贵模型到底差在哪儿?一份关于Claude与GPT分级体系的深度梳理
后端·python
玩美移动13 小时前
AI Skin Analysis API 技术解析:文件上传、异步任务与结果读取
java·人工智能·python
l1t13 小时前
编译测试兼容gzip并行压缩解压工具pigz
c++·算法
j7~13 小时前
【Python】(篇六)《基础语法(函数、列表和元组、字典、文件)》---详解
开发语言·python·文件·函数·字典·编程学习·元组与列表
代码不停14 小时前
BFS算法解决FloodFill问题
算法·bfs·宽度优先