無論人們多少次寫道驗證碼早已過時,不再像開發者最初希望的那樣有效,然而,互聯網資源的所有者仍然繼續用驗證碼保護他們的項目。但我們這個時代最受歡迎的驗證碼是什麼?
澄清 - 本文中介紹的所有程式碼都是基於驗證碼識別服務 2captcha 的 API 文件編寫的
這是驗證碼。 Recaptcha V2、V3 等,由 Google 於 2007 年創建。自從第一個 recaptcha 出現以來已經很多年了,但它仍然保持著花環,週期性地輸給競爭對手,然後又贏回來。但儘管 recapcha 在神經網路面前有許多缺陷,但它的受歡迎程度從未達到第二位。
人們曾經進行過大量創建「驗證碼殺手」的嘗試,有些不太成功,有些看起來只是對驗證碼的威脅,但事實上卻毫無作用。但事實仍然是,競爭對手希望做比 recapcha 更好、更可靠的事情,這表明了它的受歡迎程度。
如果您不信任任何第三方模組,我已經準備了最通用的程式碼,只需稍作修改即可插入您的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 金鑰,當然,也要安裝必要的模組。
與 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 函數,例如 file_get_contents 和 json_decode,以下是程式碼本身:
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中文網其他相關文章!