You need to enable JavaScript to run this app. Selenium爬取Ozon页面遇CORB跨源拦截致第二页解析失败,如何用Python配置解决
优惠活动
大模型
产品
解决方案
定价
更多

Selenium爬取Ozon页面遇CORB跨源拦截致第二页解析失败,如何用Python配置解决

Selenium 配置禁用CORB跨源拦截的操作方法

核心配置实现

不需要安装额外第三方工具,直接通过Selenium原生的ChromeOptions类注入验证过的启动参数即可,步骤如下:

  • 移除原有直接初始化驱动的代码,导入Chrome选项配置模块
  • 在选项配置中添加两个禁用CORB的开关参数
  • 将配置对象传入驱动初始化方法

可直接复用的修改后完整代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

URL = "https://www.ozon.ru/category/elektroinstrumenty-9857/"

# 初始化Chrome启动参数
chrome_options = Options()
# 注入禁用CORB特性的参数
chrome_options.add_argument("--disable-features=CrossSiteDocumentBlockingAlways,CrossSiteDocumentBlockingIfIsolating")
# 补充基础反爬配置,减少站点对自动化工具的识别
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option("useAutomationExtension", False)

# 带配置启动驱动
driver = webdriver.Chrome(options=chrome_options)
driver.get(URL)


def parse_page(driver):
    res = []
    products = driver.find_elements_by_xpath("/html/body/div[1]/div/div[1]/div[3]/div[2]/div[2]/div[3]/div[1]/div/div/div/div/a")
    print(len(products))
    for product in products:
        res.append(product.get_attribute('href'))
    return res

def get_links(driver, category="akkumulyatornye-otvertki-9902"):
    res = []
    for i in range(1, 10):
        if i == 1:
            url = "https://www.ozon.ru/category/{}/".format(category)
        else:
            url = "https://www.ozon.ru/category/{}/?page={}".format(category, i)
        driver.get(url)
        cur_links = parse_page(driver)
        res.append(cur_links)
        if len(cur_links) < 30:
            break
        time.sleep(6)
    return res

ll = get_links(driver, category="akkumulyatornye-otvertki-9902")

关键注意事项

  • 注入参数时不要带上快捷方式中的%U占位符,这个值是系统调用Chrome时的路径占位符,在Selenium环境下传入会导致参数解析报错、Chrome启动失败
  • 日志中被拦截的composer_events是Ozon的前端埋点上报接口,CORB本身不会拦截页面商品列表的HTML内容,你之前无法定位第二页元素的本质原因是:埋点请求被拦截后,Ozon前端会触发反爬校验逻辑,主动阻断后续分页内容的DOM渲染,禁用CORB开关后埋点请求正常放行,DOM渲染流程就不会被打断
  • 原有代码中导入的requests模块未被实际使用,可以直接删除减少冗余
  • 当前Selenium 4.x版本对ChromeOptions的原生支持是完整的,不需要额外安装适配工具即可生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.08 09:27:04