Python Requests提交表单遇reCAPTCHA,如何获取g-recaptcha-response?
g-recaptcha-response for Invisible reCAPTCHA with Python Hey there, let's break down how to solve this reCAPTCHA roadblock—it's the final piece to get your script working, so let's dive in!
First, Why Your Current Approach Isn't Working
The g-recaptcha-response value isn't present in the static HTML you fetch with requests and parse with BeautifulSoup because it's generated dynamically by Google's JavaScript after a user completes the reCAPTCHA verification. Since requests doesn't execute JavaScript, you can't grab this token directly from the initial page load. Also, note that you were mistakenly passing the data-sitekey to the siteverify API—this is wrong: the siteverify endpoint expects the actual verification token (the g-recaptcha-response value), not the sitekey.
Solutions to Get the Token
To get the valid g-recaptcha-response token, you need to simulate a real browser environment that can execute JavaScript and handle the reCAPTCHA flow. Here are the two most reliable methods:
1. Use Selenium (Most Common & Straightforward)
Selenium lets you control a real browser, which will execute the reCAPTCHA JavaScript and let you capture the token after verification. Here's a step-by-step example:
First, install Selenium and download the appropriate browser driver (e.g., ChromeDriver for Chrome):
pip install selenium
Then, modify your script to use Selenium:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import requests # Initialize the browser (Chrome in this example) driver = webdriver.Chrome() link_checkout = "YOUR_CHECKOUT_URL_HERE" driver.get(link_checkout) # Trigger the invisible reCAPTCHA verification # For invisible reCAPTCHA, it's usually tied to a submit button click try: submit_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']") submit_button.click() except: # If clicking the button doesn't trigger it, manually execute the reCAPTCHA script driver.execute_script("grecaptcha.execute();") # Wait for the verification to complete and the token to be populated wait = WebDriverWait(driver, 15) captcha_textarea = wait.until( EC.presence_of_element_located((By.ID, "g-recaptcha-response")) ) captcha_token = captcha_textarea.get_attribute("value") # Now use this token in your payload payload_FORM = { # Your existing form fields here "g-recaptcha-response": captcha_token } # You can either submit the form via Selenium or use requests # Option 1: Submit via Selenium # driver.find_element(By.CSS_SELECTOR, "form").submit() # Option 2: Submit via requests (reuse your existing session if needed) c = requests.Session() c.post("YOUR_FORM_SUBMIT_URL", data=payload_FORM, headers={"Referer": link_checkout}) # Clean up driver.quit()
2. Use Playwright (Modern Alternative to Selenium)
Playwright is a newer tool that offers better browser automation and built-in waits. The approach is similar to Selenium, but with cleaner syntax:
Install Playwright and set up browsers:
pip install playwright playwright install
Example code:
from playwright.sync_api import sync_playwright import requests with sync_playwright() as p: browser = p.chromium.launch(headless=False) # Set headless=True for background execution page = browser.new_page() link_checkout = "YOUR_CHECKOUT_URL_HERE" page.goto(link_checkout) # Trigger reCAPTCHA page.click("button[type='submit']") # Wait for the token to be populated page.wait_for_selector("#g-recaptcha-response", state="visible") captcha_token = page.evaluate("document.getElementById('g-recaptcha-response').value") # Build payload and submit payload_FORM = {**YOUR_EXISTING_PAYLOAD, "g-recaptcha-response": captcha_token} c = requests.Session() c.post("YOUR_FORM_SUBMIT_URL", data=payload_FORM, headers={"Referer": link_checkout}) browser.close()
Important Notes
- Compliance: Automating reCAPTCHA may violate the target website's Terms of Service. Make sure you have explicit permission to scrape or automate actions on the site.
- Invisible reCAPTCHA Variations: Some implementations require calling
grecaptcha.execute()directly if the submit button doesn't trigger it. Use theexecute_script(Selenium) orevaluate(Playwright) method to run this JS function if needed. - Siteverify API: Once you have the token, you can verify it using Google's
siteverifyAPI (with your secret key) to ensure it's valid before submitting the form, but this is optional if the site handles verification server-side.
内容的提问来源于stack exchange,提问作者lucky simon

