gpt4 book ai didi

javascript - 如何使用 python 截取给定 url 的屏幕截图

转载 作者:太空宇宙 更新时间:2023-11-03 23:51:10 25 4
gpt4 key购买 nike

我正在尝试从给定的 URL 截取屏幕截图。在 javascript 中尝试了 html2canvas 库,因为它不支持某些 CSS 格式而被放弃。现在尝试使用 python 和 selenium 或任何其他库(如果可能)捕获给定 URL 的屏幕截图。

我已经完成了以前的解决方案,我所面临的是,

1.pyqt4 - 即使在安装 pyqt4 后仍面临 No module named 'PyQt4.QtWebKit' 错误

2.selenium - 代码没有截取整个页面滚动。

3.phantom.js - 为某些网站提供浮点转储错误

Selenium 示例代码:

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

options = Options()
options.add_argument("--headless") # Runs Chrome in headless mode.
options.add_argument('--no-sandbox') # # Bypass OS security model
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path='./chromedriver')
driver.get('https://stackoverflow.com/questions/51000899/better-way-to-take-screenshot-of-a-url-in-python')
driver.save_screenshot('screenshot-headless.png')
driver.quit()

环境:

操作系统:ubuntu 18.04

python :3.6

预期输出:(任何一个)

1.抓取图片的Dataurl

2.捕获的图像(通过滚动)

我的代码有什么问题?有没有其他选择?

最佳答案

你尝试过使用 Pyppeteer https://github.com/miyakogi/pyppeteer

使用 fullPage 参数,您可以获取整个页面的屏幕截图。

import asyncio
from pyppeteer import launch

async def main():
browser = await launch(headless=True)
page = await browser.newPage()

await page.goto('https://stackoverflow.com/questions/51000899/better-way-to-take-screenshot-of-a-url-in-python')
await page.screenshot({'path': 'screen.png', 'fullPage': True})
await browser.close()


asyncio.get_event_loop().run_until_complete(main())

编辑

https://github.com/miyakogi/pyppeteer没有维护。新项目:https://github.com/pyppeteer/pyppeteer

关于javascript - 如何使用 python 截取给定 url 的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59321878/

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