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

如何用Python Selenium加载并爬取索尼相机网页的规格标签内容?

解决索尼相机网页规格内容爬取问题

需要爬取索尼全画幅相机(页面地址:https://electronics.sony.com/imaging/interchangeable-lens-cameras/full-frame/p/ilce7rm4-b)的规格信息,该内容需要先点击页面上的Specification标签,再点击see more按钮才能完全加载。但现有的Selenium Python脚本执行失败,以下是尝试的代码:

def specs_see_more(driver):
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH,
                                        '//*[@id="PDPSpecificationsLink"]')))
    time.sleep(3)
    see_more_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH,
                                    '//*[@id="PDPSpecificationsLink"]')))

    driver.execute_script("arguments[0].scrollIntoView(true);", see_more_button)

    WebDriverWait(driver, 10).until(
        EC.visibility_of(see_more_button))

    see_more_button.click()

    spec_page_source = driver.page_source
    driver_soup = BeautifulSoup(spec_page_source, "html.parser")
    print(driver_soup)

问题分析与修正方案

1. 缺失Specification标签的点击步骤

原代码直接定位see more按钮,但页面默认不在Specification标签页,必须先切换到该标签页才能看到目标按钮。需先添加标签点击逻辑:

# 先点击Specification标签
spec_tab = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//a[contains(text(), "Specification")]'))
)
spec_tab.click()

2. 修正元素定位的转义问题

原代码中XPATH里的"是HTML转义字符,Python代码中应直接使用双引号,比如将'//*[@id="PDPSpecificationsLink"]'改为'//*[@id="PDPSpecificationsLink"]'。

3. 优化等待逻辑,移除不必要的sleep

用显式等待替代固定时长的time.sleep(3),既提升效率也能避免等待不足的问题。

4. 处理元素遮挡问题

若按钮被导航栏、弹窗等遮挡,原生点击可能失效,改用JS触发点击更可靠:

driver.execute_script("arguments[0].click();", see_more_button)

修正后的完整代码

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
from bs4 import BeautifulSoup

def specs_see_more(driver):
    # 切换到Specification标签页
    spec_tab = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//a[contains(text(), "Specification")]'))
    )
    spec_tab.click()
    
    # 定位并等待see more按钮可点击
    see_more_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="PDPSpecificationsLink"]'))
    )
    
    # 滚动到按钮居中位置,避免被遮挡
    driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", see_more_button)
    
    # 用JS点击按钮
    driver.execute_script("arguments[0].click();", see_more_button)
    
    # 等待规格内容加载完成
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CLASS_NAME, "specifications-container"))
    )
    
    # 解析并提取规格内容
    spec_page_source = driver.page_source
    driver_soup = BeautifulSoup(spec_page_source, "html.parser")
    specs_content = driver_soup.find(class_="specifications-container")
    print(specs_content.get_text(strip=True))

# 调用示例
if __name__ == "__main__":
    driver = webdriver.Chrome()
    driver.get("https://electronics.sony.com/imaging/interchangeable-lens-cameras/full-frame/p/ilce7rm4-b")
    specs_see_more(driver)
    driver.quit()

额外注意事项

  • 确保浏览器驱动版本与浏览器版本匹配,避免兼容性报错。
  • 若页面有反爬机制,可添加自定义User-Agent头,或模拟随机滚动、短暂等待等用户行为。
  • 若页面DOM结构更新(如specifications-container类名变化),需同步调整元素定位方式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 05:37:18