You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Selenium绕过新加坡交易所的reCAPTCHA反爬机制?

解决Selenium访问SGX网站时reCAPTCHA验证无法绕过的问题

我尝试用Selenium访问新加坡交易所(SGX)的公司公告页面,但页面一直弹出reCAPTCHA验证:
reCAPTCHA验证弹窗

我已经使用了selenium_recaptcha_solver工具,但问题还是没解决,求可行的解决方案。

我的代码如下:

from selenium_recaptcha_solver import RecaptchaSolver
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


test_ua = 'Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36'

options = Options()

options.add_argument("--headless")  # Remove this if you want to see the browser (Headless makes the chromedriver not have a GUI)
options.add_argument("--window-size=1920,1080")

options.add_argument(f'--user-agent={test_ua}')

options.add_argument('--no-sandbox')
options.add_argument("--disable-extensions")

test_driver = webdriver.Chrome(options=options)

solver = RecaptchaSolver(driver=test_driver)

test_driver.get('https://www.sgx.com/securities/company-announcements')

recaptcha_iframe = test_driver.find_element(By.XPATH, '//iframe[@title="reCAPTCHA"]')

solver.click_recaptcha_v2(iframe=recaptcha_iframe)

可行解决方案

1. 关闭Headless模式

Headless模式极易被反爬机制识别,直接移除--headless参数,使用可视化浏览器运行,模拟真实用户的操作环境。

2. 优化浏览器配置,增强伪装

  • 替换为当前主流浏览器的User-Agent,比如最新Chrome的UA:
    test_ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
    
  • 添加屏蔽自动化检测的参数:
    options.add_argument("--disable-blink-features=AutomationControlled")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    
  • 加载本地浏览器的默认用户配置文件,让环境更贴近普通用户:
    # Windows下示例路径,替换为你自己的Chrome用户数据路径
    options.add_argument(r"--user-data-dir=C:\Users\你的用户名\AppData\Local\Google\Chrome\User Data")
    options.add_argument(r'--profile-directory=Default')
    

3. 模拟真实用户的操作延迟

在页面加载、元素交互前添加随机延迟,避免机械性操作被检测:

import time
import random

test_driver.get('https://www.sgx.com/securities/company-announcements')
time.sleep(random.uniform(2, 5))  # 随机等待2-5秒

recaptcha_iframe = test_driver.find_element(By.XPATH, '//iframe[@title="reCAPTCHA"]')
time.sleep(random.uniform(1, 3))
solver.click_recaptcha_v2(iframe=recaptcha_iframe)

4. 改用第三方验证码识别服务

如果自动solver失效,可以使用专业的验证码识别服务(如2Captcha),这类服务通过人工或AI识别验证码,成功率更高。示例代码:

# 先安装依赖:pip install 2captcha-python
from twocaptcha import TwoCaptcha

# 替换为你的2Captcha API密钥
api_key = "你的API密钥"
solver = TwoCaptcha(api_key)

# 从SGX页面的reCAPTCHA元素中提取sitekey
sitekey = "6LfKURIUAAAAAH1Q6cP_7yW246Wpbd3Pm6E8r0wK"
url = "https://www.sgx.com/securities/company-announcements"

try:
    result = solver.recaptcha(sitekey=sitekey, url=url)
    code = result['code']
    # 将识别结果注入页面的验证码响应字段
    test_driver.execute_script(f'document.getElementById("g-recaptcha-response").innerHTML="{code}";')
    # 提交验证(如果页面有提交按钮)
    submit_btn = test_driver.find_element(By.XPATH, '//button[@type="submit"]')
    submit_btn.click()
except Exception as e:
    print(e)

5. 用selenium-stealth增强伪装

该库可以模拟更真实的浏览器环境,规避WebDriver检测:

# 安装依赖:pip install selenium-stealth
from selenium_stealth import stealth

stealth(test_driver,
        languages=["en-US", "en"],
        vendor="Google Inc.",
        platform="Win32",
        webgl_vendor="Intel Inc.",
        renderer="Intel Iris OpenGL Engine",
        fix_hairline=True,
        )

内容的提问来源于stack exchange,提问作者StudyOnly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 08:46:38