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

如何滚动至页面末尾获取全部产品?当前仅能获取前20个产品

页面滚动加载产品失败,仅能获取前20个产品的解决办法

我尝试从URL xyz 获取并打印所有列出的产品,但遇到滚动加载问题——页面只加载前20个产品,剩余内容无法获取。我试过的代码如下:

last_height = driver.execute_script("return document.body.scrollHeight")

# while True:
#     driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
#     time.sleep(20)  # Wait for the page to load new content
#     new_height = driver.execute_script("return document.body.scrollHeight")
#     if new_height == last_height:
#         break
#     last_height = new_height

以下是几种针对性的解决思路:

  • 滚动到可见元素而非页面底部
    部分页面的动态加载触发逻辑是元素可见性,而非页面高度变化。可以定位当前页面最后一个产品元素,滚动到它的位置来触发加载:

    from selenium.webdriver.common.action_chains import ActionChains
    
    while True:
        products = driver.find_elements_by_css_selector("替换为你的产品元素选择器")
        # 滚动到最后一个产品
        last_product = products[-1]
        ActionChains(driver).move_to_element(last_product).perform()
        time.sleep(3)  # 根据页面加载速度调整等待时间
        new_products = driver.find_elements_by_css_selector("替换为你的产品元素选择器")
        if len(new_products) == len(products):
            break
    
  • 替换固定等待为显式等待
    固定time.sleep(20)过于死板,改用显式等待检测新内容加载状态,既避免等待不足也减少不必要的等待:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    last_height = driver.execute_script("return document.body.scrollHeight")
    while True:
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        # 等待页面高度变化或超时
        try:
            WebDriverWait(driver, 10).until(
                lambda d: d.execute_script("return document.body.scrollHeight") > last_height
            )
        except:
            break
        last_height = driver.execute_script("return document.body.scrollHeight")
    
  • 检查是否存在「加载更多」按钮
    有些页面不会自动滚动加载,需要手动点击「加载更多」按钮触发内容加载,先确认页面是否有这类元素:

    while True:
        try:
            load_more_btn = driver.find_element_by_css_selector("替换为加载按钮的选择器")
            load_more_btn.click()
            time.sleep(3)
        except:
            break
    
  • 处理无限滚动的特殊场景
    部分页面的document.body.scrollHeight不会随滚动变化,而是通过动态调整元素位置实现无限滚动,这时可以按像素逐步滚动:

    scroll_step = 1000
    current_scroll = 0
    while True:
        driver.execute_script(f"window.scrollTo(0, {current_scroll});")
        time.sleep(2)
        current_scroll += scroll_step
        # 检测是否到达页面底部
        bottom_reached = driver.execute_script("return window.innerHeight + window.pageYOffset >= document.body.scrollHeight")
        if bottom_reached:
            # 额外等待几秒确认是否有新内容加载
            time.sleep(3)
            new_bottom_reached = driver.execute_script("return window.innerHeight + window.pageYOffset >= document.body.scrollHeight")
            if new_bottom_reached:
                break
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 04:57:05