1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

🤖 AI × 2CaptchaでreCAPTCHAを完全自動化!🚀

Posted at

🚀 2Captcha APIを使ってreCAPTCHAを自動化する方法

image.png

📌 はじめに

ウェブサイトをスクレイピングや自動化する際、多くのサイトがreCAPTCHAを導入しており、通常のボットでは通過できません。しかし、2Captcha APIを使用すると、reCAPTCHAを自動で解決し、人間のように操作できるようになります。今回は、PythonとSeleniumを使用して、2Captcha APIを使ったreCAPTCHAの自動化方法を詳しく解説します。🚀

🎯 方法の概要

この方法では、以下の手順でreCAPTCHAを自動化します。

  1. 2Captcha APIに登録し、APIキーを取得する 🔑
  2. PythonのSeleniumを使用してウェブサイトを開く 🌐
  3. 2Captcha APIを使ってreCAPTCHAを解決し、取得したトークンを入力する 🔄
  4. フォームを送信し、成功を確認する

それでは、1つずつ詳しく説明していきます!


🚀 2Captcha APIの登録とAPIキーの取得

🔹 1. 2Captchaアカウントの作成

まずは、2Captcha にアクセスし、無料アカウントを作成しましょう。

🔹 2. APIキーの取得

  1. ログイン したら、ダッシュボードに移動。
  2. APIキー をコピーします(これをスクリプトで使用します)。
  3. 必要に応じて、アカウントにクレジットを追加(有料機能を使う場合)。

これで、準備完了です!✨


🛠️ 必要なライブラリのインストール

次に、Pythonの必要なライブラリをインストールしましょう。

pip install selenium requests webdriver-manager

📌 ライブラリの用途:

  • selenium → ブラウザの自動操作を行う。
  • requests → 2Captcha APIと通信するために使用。
  • webdriver-manager → Selenium用のドライバーを管理。

🏗️ SeleniumでreCAPTCHAのあるページを開く

まずは、Seleniumを使ってターゲットのウェブサイトを開きましょう。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time

# Chromeを起動
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)

# reCAPTCHAのあるウェブサイトを開く
driver.get("https://www.example.com")
time.sleep(3)  # ページの読み込みを待つ

🔹 このコードの動作:

  • SeleniumでChromeブラウザを開き、ターゲットのウェブサイトにアクセス。
  • time.sleep(3) でページの読み込みを待つ。

🔄 2Captcha APIを使ってreCAPTCHAを解決する

次に、2Captcha APIを使ってreCAPTCHAを解決し、取得したトークンをページに入力します。

🔹 1. reCAPTCHAのサイトキーを取得

ターゲットのウェブサイトのreCAPTCHAのサイトキーを取得する必要があります。

recaptcha_sitekey = driver.find_element(By.XPATH, "//div[@class='g-recaptcha']").get_attribute("data-sitekey")

🔹 2. 2Captchaにリクエストを送信

import requests

API_KEY = "your_2captcha_api_key"
URL = "https://2captcha.com/in.php"

payload = {
    "key": API_KEY,
    "method": "userrecaptcha",
    "googlekey": recaptcha_sitekey,
    "pageurl": "https://www.example.com",
    "json": 1
}

response = requests.post(URL, data=payload)
captcha_id = response.json().get("request")

🔹 このコードの動作:

  • requests.post() を使って、2CaptchaにreCAPTCHAの解決をリクエスト。
  • 返ってきたcaptcha_idを取得し、解決結果を取得するために使用。

🔹 3. reCAPTCHAのトークンを取得

token_url = f"https://2captcha.com/res.php?key={API_KEY}&action=get&id={captcha_id}&json=1"
while True:
    token_response = requests.get(token_url).json()
    if token_response["status"] == 1:
        recaptcha_token = token_response["request"]
        break
    time.sleep(5)  # 5秒ごとにトークンを取得するまで待つ

🔹 このコードの動作:

  • requests.get() を使って、2Captchaが解決したトークンを取得。
  • トークンがまだ取得できない場合、5秒待って再試行。

🔹 4. トークンをreCAPTCHAに入力し、送信

recaptcha_response = driver.execute_script(f"document.getElementById('g-recaptcha-response').innerHTML = '{recaptcha_token}';")
submit_button = driver.find_element(By.ID, "submit")
submit_button.click()
time.sleep(5)

🔹 このコードの動作:

  • JavaScriptを使って、reCAPTCHAのレスポンスフィールドにトークンを設定。
  • フォームを送信して、認証を完了。

🎉 まとめ

2Captcha APIを使ってreCAPTCHAを自動で解決できるようになりました!

💡 学んだこと:

  • Seleniumを使ってブラウザを操作。
  • 2Captcha APIを使ってreCAPTCHAを解決。
  • 取得したトークンを入力し、自動認証を完了。

🔮 次のステップ:

実は、私はAIをトレーニングしてreCAPTCHAを解決することにも挑戦しています!🤖

次回は、AIエージェントを使ってreCAPTCHAをより高度に自動化する方法を紹介します!

📢 ぜひフォローして、一緒に学んでいきましょう!🚀

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?