gpt4 book ai didi

python - 如何使用 Python 截取整个滚动元素的屏幕截图

转载 作者:行者123 更新时间:2023-11-28 18:17:08 27 4
gpt4 key购买 nike

我需要截取整个网页。这里的重要部分是我需要屏幕截图来包含屏幕上不适合的页面的全部内容。

数据包含多行(rows)数据,由于数据长度较长,有滚动条。行数每次都不同,屏幕截图应相应。

对于滚动的长网页,执行此任务是微不足道的。但是当数据量很大,滚到滚动条下怎么实现呢。

我想使用 Python 来完成这个。我正在使用以下代码使用 Python 捕获网页的屏幕截图。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1440x1440')
driver = webdriver.Chrome(executable_path=os.path.abspath('C:/Program Files (x86)/Python36/selenium/chromedriver/build/scripts-3.6/chromedriver.exe'),chrome_options=options)
driver.get("https://www.test.com") ##updated as a random test URL
time.sleep(60);
driver.save_screenshot('C:/Users/Dev/Desktop/Maxx/Snapshots/test.png')
driver.quit
print ("captured snapshot")

有关它在带有滚动条的浏览器上的外观的数据。

enter image description here

最佳答案

from PIL import Image
from io import BytesIO

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

def open_url(url):
options = Options()

options.headless = True

driver = webdriver.Chrome(chrome_options=options)

driver.maximize_window()
driver.get(url)
save_screenshot(driver, 'screen.png')

def save_screenshot(driver, file_name):
height, width = scroll_down(driver)
driver.set_window_size(width, height)
img_binary = driver.get_screenshot_as_png()
img = Image.open(BytesIO(img_binary))
img.save(file_name)
# print(file_name)
print(" screenshot saved ")


def scroll_down(driver):
total_width = driver.execute_script("return document.body.offsetWidth")
total_height = driver.execute_script("return document.body.parentNode.scrollHeight")
viewport_width = driver.execute_script("return document.body.clientWidth")
viewport_height = driver.execute_script("return window.innerHeight")

rectangles = []

i = 0
while i < total_height:
ii = 0
top_height = i + viewport_height

if top_height > total_height:
top_height = total_height

while ii < total_width:
top_width = ii + viewport_width

if top_width > total_width:
top_width = total_width

rectangles.append((ii, i, top_width, top_height))

ii = ii + viewport_width

i = i + viewport_height

previous = None
part = 0

for rectangle in rectangles:
if not previous is None:
driver.execute_script("window.scrollTo({0}, {1})".format(rectangle[0], rectangle[1]))
time.sleep(0.5)
# time.sleep(0.2)

if rectangle[1] + viewport_height > total_height:
offset = (rectangle[0], total_height - viewport_height)
else:
offset = (rectangle[0], rectangle[1])

previous = rectangle

return (total_height, total_width)

open_url("https://www.medium.com")

scroll_down函数滚动到页面底部并返回网页的总高度和宽度。

save_screenshot 函数设置窗口大小并使用 pillow 保存屏幕截图。

关于python - 如何使用 Python 截取整个滚动元素的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47633765/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com