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

Python网页爬虫登录后遭人机验证拦截的解决方法

解决Selenium爬虫登录时的人机验证拦截问题

网站拦截提示

访问此页面已被拒绝,因为我们认为您正在使用自动化工具浏览网站。可能由以下原因导致:
JavaScript被禁用或被扩展程序(例如广告拦截器)阻止
您的浏览器不支持Cookie
请确保浏览器中已启用JavaScript和Cookie,且未阻止它们加载。

现有代码的核心问题

代码仅配置了基础的User-Agent和JS启用,缺少规避自动化检测的关键配置,且依赖固定time.sleep的操作逻辑不够可靠,容易被网站识别为爬虫。

优化方案及代码调整

通过以下配置和代码修改,模拟更真实的浏览器行为,降低被检测概率:

1. 增强EdgeOptions配置

添加规避自动化检测、启用Cookie、禁用干扰扩展等参数,让浏览器环境更贴近真实用户。

2. 用显式等待替代固定sleep

避免因网络延迟导致的元素未加载问题,操作逻辑更可靠。

3. 隐藏WebDriver标识

网站常通过navigator.webdriver属性识别自动化工具,需手动覆盖该属性。

优化后的完整代码

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 random
import time

def login(login_page, my_username, my_password):
    user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"

    options = webdriver.EdgeOptions()
    # 基础环境配置
    options.add_argument(f"user-agent={user_agent}")
    options.add_argument("--enable-javascript")
    options.add_argument("--accept-cookies")
    # 规避自动化检测核心配置
    options.add_argument("--disable-blink-features=AutomationControlled")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    # 消除干扰项
    options.add_argument("--disable-extensions")
    options.add_argument("--disable-popup-blocking")
    options.add_argument("--start-maximized")  # 最大化窗口,模拟真实用户操作

    driver = webdriver.Edge(options=options)
    # 隐藏webdriver属性
    driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")

    driver.get(login_page)

    # 等待用户名输入框加载完成并输入
    username_input = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "username"))
    )
    username_input.send_keys(my_username)
    time.sleep(random.uniform(0.5, 1.5))  # 模拟人类输入间隔

    # 等待登录按钮可点击并点击
    login_btn = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, 'loginbutton'))
    )
    login_btn.click()

    # 等待密码输入框出现并输入
    password_input = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "password"))
    )
    password_input.send_keys(my_password)
    time.sleep(random.uniform(0.5, 1.5))

    # 再次点击登录按钮
    login_btn = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, 'loginbutton'))
    )
    login_btn.click()

    return driver

额外建议

  • 加载用户配置文件:如果上述配置仍被检测,可添加用户数据目录参数,使用日常使用的Edge浏览器环境登录:
    options.add_argument(r"user-data-dir=C:\Users\你的用户名\AppData\Local\Microsoft\Edge\User Data")
    
    替换为自己的Edge用户数据路径,启动前需关闭所有Edge窗口。
  • 控制请求频率:在操作间添加随机延迟,避免连续快速操作,更贴近人类行为。
  • 检查浏览器环境:确保没有广告拦截器等扩展干扰,保持浏览器环境干净。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 16:03:12