Python网页滚动截图:滚动距离不准致重复/漏截问题求助
精准滚动页面截图:解决重复/漏截问题
问题背景
用Python编写程序,打开指定链接后滚动页面截图用于后续OCR识别。目前程序可完成页面打开、截图及滚动至页底的操作,但存在两个核心问题:
- 无法精准按浏览器窗口高度滚动,导致前后截图出现内容重复(前一张末尾与后一张开头重叠)
- 滚动距离偶尔超出窗口高度,造成文本漏截
现有实现代码
Selenium核心代码
from selenium import webdriver import time # 创建Chrome驱动实例 driver = webdriver.Chrome() # 打开目标网页 driver.get("https://www.stoiximan.gr/sport/basket/") # 等待页面加载完成 time.sleep(5) # 最大化窗口 driver.maximize_window() # 等待几秒 time.sleep(2) # 获取浏览器窗口高度 window_height = driver.execute_script("return window.innerHeight") print(window_height) # 初始化滚动位置为0 scroll_position = 0 # 循环直到页面底部 while True: # 截取当前页面 screenshot_name = f"screenshot_{scroll_position}.png" driver.save_screenshot(screenshot_name) # 滚动窗口高度的距离 scroll_position += window_height driver.execute_script(f"window.scrollTo(0, {scroll_position});") # 等待几秒 time.sleep(2) # 判断是否到达页面底部 if scroll_position >= driver.execute_script("return document.body.scrollHeight"): break # 关闭浏览器 driver.quit()
尝试过的tkinter屏幕高度获取代码
import tkinter as tk # 创建tkinter根窗口 root = tk.Tk() # 获取屏幕高度 screen_height = root.winfo_screenheight() print(f"屏幕高度: {screen_height} 像素") # 销毁根窗口 root.destroy()
解决方案
问题根源在于页面高度计算不准确、滚动逻辑未考虑动态加载内容,以及截图范围未严格匹配可视区域。以下是修正后的实现:
方案1:精准滚动逻辑优化
from selenium import webdriver import time driver = webdriver.Chrome() driver.get("https://www.stoiximan.gr/sport/basket/") time.sleep(5) driver.maximize_window() time.sleep(2) # 获取初始可视窗口高度 window_height = driver.execute_script("return window.innerHeight") # 用documentElement.scrollHeight获取更准确的页面总高度 total_height = driver.execute_script("return document.documentElement.scrollHeight") scroll_position = 0 while scroll_position < total_height: # 截图当前页面 driver.save_screenshot(f"screenshot_{scroll_position}.png") # 计算下一次滚动位置,避免超出页面总高度 next_scroll = scroll_position + window_height if next_scroll > total_height: next_scroll = total_height # 执行滚动 driver.execute_script(f"window.scrollTo(0, {next_scroll});") scroll_position = next_scroll # 等待懒加载内容加载完成 time.sleep(2) # 更新页面总高度(滚动后可能加载新内容) total_height = driver.execute_script("return document.documentElement.scrollHeight") driver.quit()
方案2:裁剪可视区域彻底避免重复
如果需要完全杜绝重复,可截取当前可视区域而非整个页面,结合PIL进行裁剪:
from selenium import webdriver import time from PIL import Image import io driver = webdriver.Chrome() driver.get("https://www.stoiximan.gr/sport/basket/") time.sleep(5) driver.maximize_window() time.sleep(2) window_height = driver.execute_script("return window.innerHeight") window_width = driver.execute_script("return window.innerWidth") total_height = driver.execute_script("return document.documentElement.scrollHeight") scroll_position = 0 while scroll_position < total_height: # 获取全屏截图 screenshot_bytes = driver.get_screenshot_as_png() img = Image.open(io.BytesIO(screenshot_bytes)) # 裁剪当前可视区域:(左, 上, 右, 下) crop_box = (0, scroll_position, window_width, scroll_position + window_height) cropped_img = img.crop(crop_box) cropped_img.save(f"screenshot_{scroll_position}.png") # 计算下一次滚动位置 next_scroll = scroll_position + window_height if next_scroll > total_height: next_scroll = total_height driver.execute_script(f"window.scrollTo(0, {next_scroll});") scroll_position = next_scroll time.sleep(2) total_height = driver.execute_script("return document.documentElement.scrollHeight") driver.quit()
内容的提问来源于stack exchange,提问作者ddd ddd
相关产品推荐
相关产品推荐

