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

Selenium无法加载商品视频元素,无法抓取视频URL求助

问题描述

尝试抓取页面 https://grabagun.com/firearms/handguns/semi-automatic-handguns/glock-19-gen-5-polished-nickel-9mm-4-02-inch-barrel-15-rounds-exclusive.html 的视频URL,但使用Selenium启动Chrome和Firefox时,页面中的视频元素div始终不显示。已尝试添加防自动化检测配置、使用Chrome默认用户配置文件,均未解决问题。相关代码如下:

基础防检测配置代码

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType

from selenium_stealth import stealth
import requests, csv, time

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options,service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))
url = "https://grabagun.com/firearms/handguns/semi-automatic-handguns/glock-19-gen-5-polished-nickel-9mm-4-02-inch-barrel-15-rounds-exclusive.html"
driver.get(url)  

使用默认用户配置文件的代码

options.add_argument("user-data-dir=C:\\Users\\JHONE\\AppData\\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(options=options,executable_path="C:\\Users\\chromedriver.exe")

请问视频元素无法显示的原因是什么?如何解决Selenium下视频元素无法显示的问题?

可能原因分析
  1. 防指纹工具未生效:代码中导入了selenium_stealth但未调用,网站仍可通过navigator.webdriver属性、Canvas/WebGL指纹等识别出自动化浏览器,进而隐藏视频元素。
  2. 懒加载触发条件未满足:视频元素可能采用懒加载机制,需要滚动到可见区域、等待页面JS完全执行才会渲染;现有逻辑未处理这类加载触发规则。
  3. 用户配置文件冲突:使用默认配置文件时,若Chrome正处于运行状态,会导致配置文件被锁定,无法加载正常用户会话,网站仍识别为陌生环境。
  4. 网站进阶反爬限制:部分电商网站会针对自动化工具限制媒体资源加载,通过请求头特征、行为模式判断出爬虫后,不返回视频资源或直接隐藏元素。
解决方案

1. 完善Selenium Stealth指纹模拟

调用selenium_stealth工具,模拟真实浏览器的各项特征,消除自动化标识:

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType

from selenium_stealth import stealth
import time

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
# 添加真实用户UA
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")

driver = webdriver.Chrome(options=options,service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))

# 配置stealth模拟真实浏览器
stealth(driver,
        languages=["en-US", "en"],
        vendor="Google Inc.",
        platform="Win32",
        webgl_vendor="Intel Inc.",
        renderer="Intel Iris OpenGL Engine",
        fix_hairline=True,
        )

url = "https://grabagun.com/firearms/handguns/semi-automatic-handguns/glock-19-gen-5-polished-nickel-9mm-4-02-inch-barrel-15-rounds-exclusive.html"
driver.get(url)
# 等待页面JS执行完成
time.sleep(5)

2. 添加懒加载触发与显式等待

针对懒加载的视频元素,先滚动到目标区域,再等待元素渲染完成:

from selenium.webdriver.common.by import By

# 替换为页面实际的视频容器选择器
video_container_selector = "div.product-video"

# 滚动到页面底部触发懒加载
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)

# 显式等待视频元素出现,最长等待10秒
try:
    video_element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, video_container_selector))
    )
    # 提取视频URL
    video_src = video_element.find_element(By.TAG_NAME, "video").get_attribute("src")
    print(f"视频URL: {video_src}")
except Exception as e:
    print(f"未找到视频元素: {e}")

3. 修复用户配置文件使用问题

  • 使用配置文件前完全关闭所有Chrome进程,避免配置文件被占用。
  • 复制默认配置文件到新路径,使用副本而非原目录,防止破坏原用户数据:
    options.add_argument("user-data-dir=C:\\Users\\JHONE\\Documents\\Chrome_Profile_Copy")
    

4. 模拟真实用户行为

添加随机停顿、鼠标移动等操作,降低被识别为爬虫的概率:

from selenium.webdriver.common.action_chains import ActionChains

# 模拟鼠标随机移动
action = ActionChains(driver)
action.move_by_offset(100, 100).perform()
time.sleep(1)
action.move_by_offset(50, 50).perform()
time.sleep(2)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 21:45:36