无论人们多少次写道验证码早已过时,不再像开发者最初希望的那样有效,但是,互联网资源的所有者仍然继续使用验证码来保护他们的项目。但我们这个时代最流行的验证码是什么?
澄清 - 本文中介绍的所有代码都是基于验证码识别服务 2captcha 的 api 文档编写的
这是验证码。 recaptcha v2、v3 等,由 google 于 2007 年创建。自第一个 recaptcha 出现以来已经很多年了,但它仍然保持着花环,周期性地输给竞争对手,然后又赢回来。但尽管 recapcha 在神经网络面前存在诸多缺陷,但它的受欢迎程度从未达到第二位。
人们曾进行过大量创建“验证码杀手”的尝试,有些不太成功,有些看起来只是对验证码的威胁,但事实上却毫无作用。但事实仍然是,竞争对手希望做比 recapcha 更好、更可靠的事情,这表明了它的受欢迎程度。
如何使用python绕过recaptcha(代码示例)
如果您不信任任何第三方模块,我已经准备了最通用的代码,只需稍作修改即可插入您的python脚本中并自动解决验证码。这是代码本身:
导入请求
导入时间
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://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: print("captcha solved successfully.") return result['request'] elif result['request'] == 'capcha_not_ready': print("the captcha has not been solved yet, waiting...") continue else: raise exception(f"error while solving captcha: {result['request']}") 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: print("captcha solved successfully.") return result['request'] elif result['request'] == 'capcha_not_ready': print("the captcha has not been solved yet, waiting...") continue else: raise exception(f"error while solving captcha: {result['request']}") # usage example for recaptcha v2 site_key_v2 = 'your_site_key_v2' url_v2 = 'https://example.com' recaptcha_token_v2 = solve_recaptcha_v2(site_key_v2, url_v2) print(f"received token for recaptcha v2: {recaptcha_token_v2}") # usage example for recaptcha v3 site_key_v3 = 'your_site_key_v3' url_v3 = 'https://example.com' recaptcha_token_v3 = solve_recaptcha_v3(site_key_v3, url_v3) print(f"received token for recaptcha v3: {recaptcha_token_v3}")
但是,在使用提供的脚本之前,请仔细阅读用于识别特定类型的验证码的服务的建议,以便了解此代码的工作原理。
另外,不要忘记在代码中插入您的 api 密钥,当然,还要安装必要的模块。
如何绕过node js中的recaptcha
与 python 的情况一样,对于那些不喜欢现成解决方案的人,下面是使用 node js 编程语言解决验证码的脚本。我提醒您不要忘记安装代码运行所需的模块,包括:
axios
您可以使用此命令安装它 –
npm 安装 axios
这是代码本身:
const axios = require('axios'); const sleep = require('util').promisify(settimeout); const api_key = 'your_api_key_2captcha'; // replace with your real api key // function for recaptcha v2 solution async function solverecaptchav2(sitekey, pageurl) { try { // sending a request for the captcha solution const sendcaptcharesponse = await axios.post(`http://2captcha.com/in.php`, null, { params: { key: api_key, method: 'userrecaptcha', googlekey: sitekey, pageurl: pageurl, json: 1 } }); if (sendcaptcharesponse.data.status !== 1) { throw new error(`error when sending captcha: ${sendcaptcharesponse.data.request}`); } const requestid = sendcaptcharesponse.data.request; console.log(`captcha sent, request id: ${requestid}`); // waiting for the captcha solution while (true) { await sleep(5000); // waiting 5 seconds before the next request 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) { console.log('captcha solved successfully.'); return getresultresponse.data.request; } else if (getresultresponse.data.request === 'capcha_not_ready') { console.log('the captcha has not been solved yet, waiting...'); } else { throw new error(`error while solving captcha: ${getresultresponse.data.request}`); } } } catch (error) { console.error(`an error occurred: ${error.message}`); } } // function for recaptcha v3 solution async function solverecaptchav3(sitekey, pageurl, action = 'verify', minscore = 0.3) { try { // sending a request for the captcha solution const sendcaptcharesponse = await axios.post(`http://2captcha.com/in.php`, null, { params: { key: api_key, method: 'userrecaptcha', googlekey: sitekey, pageurl: pageurl, version: 'v3', action: action, min_score: minscore, json: 1 } }); if (sendcaptcharesponse.data.status !== 1) { throw new error(`error when sending captcha: ${sendcaptcharesponse.data.request}`); } const requestid = sendcaptcharesponse.data.request; console.log(`captcha sent, request id: ${requestid}`); // waiting for the captcha solution while (true) { await sleep(5000); // waiting 5 seconds before the next request 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) { console.log('captcha solved successfully.'); return getresultresponse.data.request; } else if (getresultresponse.data.request === 'capcha_not_ready') { console.log('the captcha has not been solved yet, waiting...'); } else { throw new error(`error while solving captcha: ${getresultresponse.data.request}`); } } } catch (error) { console.error(`an error occurred: ${error.message}`); } } // usage example for recaptcha v2 (async () => { const sitekeyv2 = 'your_site_key_v2'; // replace with the real site key const pageurlv2 = 'https://example.com '; // replace with the real url of the page const tokenv2 = await solverecaptchav2(sitekeyv2, pageurlv2); console.log(`received token for recaptcha v2: ${tokenv2}`); })(); // usage example for recaptcha v3 (async () => { const sitekeyv3 = 'your_site_key_v3'; // replace with the real site key const pageurlv3 = 'https://example.com '; // replace with the real url of the page const action = 'homepage'; // replace with the corresponding action const minscore = 0.5; // set the minimum allowed score const tokenv3 = await solverecaptchav3(sitekeyv3, pageurlv3, action, minscore); console.log(`received token for recaptcha v3: ${tokenv3}`); })();
另外,不要忘记将您的 api 密钥插入代码中,而不是
“'your_api_key_2captcha'”
如何在 php 中识别 recapcha
好了,对于那些不习惯使用现成模块的人来说,这里是直接集成的代码。该代码使用标准 php 函数,例如 file_get_contents 和 json_decode,以下是代码本身:
<?php
function solveRecaptchaV2($apiKey, $siteKey, $url) { $requestUrl = "http://2captcha.com/in.php?key={$apiKey}&method=userrecaptcha&googlekey={$siteKey}&pageurl={$url}&json=1"; $response = file_get_contents($requestUrl); $result = json_decode($response, true); if ($result['status'] != 1) { throw new Exception("Error when sending captcha: " . $result['request']); } $captchaId = $result['request']; while (true) { sleep(5); $resultUrl = "http://2captcha.com/res.php?key={$apiKey}&action=get&id={$captchaId}&json=1"; $response = file_get_contents($resultUrl); $result = json_decode($response, true); if ($result['status'] == 1) { return $result['request']; } elseif ($result['request'] == 'CAPCHA_NOT_READY') { continue; } else { throw new Exception("Error while solving captcha: " . $result['request']); } } } function solveRecaptchaV3($apiKey, $siteKey, $url, $action = 'verify', $minScore = 0.3) { $requestUrl = "http://2captcha.com/in.php?key={$apiKey}&method=userrecaptcha&googlekey={$siteKey}&pageurl={$url}&version=v3&action={$action}&min_score={$minScore}&json=1"; $response = file_get_contents($requestUrl); $result = json_decode($response, true); if ($result['status'] != 1) { throw new Exception("Error when sending captcha: " . $result['request']); } $captchaId = $result['request']; while (true) { sleep(5); $resultUrl = "http://2captcha.com/res.php?key={$apiKey}&action=get&id={$captchaId}&json=1"; $response = file_get_contents($resultUrl); $result = json_decode($response, true); if ($result['status'] == 1) { return $result['request']; } elseif ($result['request'] == 'CAPCHA_NOT_READY') { continue; } else { throw new Exception("Error while solving captcha: " . $result['request']); } } } // Usage example for reCAPTCHA v2 $apiKey = 'YOUR_API_KEY_2CAPTCHA'; $siteKeyV2 = 'YOUR_SITE_KEY_V2'; $urlV2 = 'https://example.com'; try { $tokenV2 = solveRecaptchaV2($apiKey, $siteKeyV2, $urlV2); echo "Received token for reCAPTCHA v2: {$tokenV2}n"; } catch (Exception $e) { echo "Error: " . $e->getMessage() . "n"; } // Usage example for reCAPTCHA v3 $siteKeyV3 = 'YOUR_SITE_KEY_V3'; $urlV3 = 'https://example.com'; $action = 'homepage'; // Specify the appropriate action $MinScore = 0.5; // Specify the minimum allowed score try { $tokenV3 = solveRecaptchaV3($apiKey, $siteKeyV3, $urlV3, $action, $minScore); echo "Received token for reCAPTCHA v3: {$tokenV3}n"; } catch (Exception $e) { echo "Error: " . $e->getMessage() . "n"; } ?> I also remind you of the need to replace some parameters in the code, in particular: $apiKey = 'YOUR_API_KEY_2CAPTCHA'; $siteKeyV2 = 'YOUR_SITE_KEY_V2'; $urlV2 = 'https://example.com';
因此,使用给出的示例,您可以解决与验证码识别相关的大部分问题。有问题可以在评论里提问!
以上就是如何绕过验证码的详细内容,更多请关注php中文网其它相关文章!