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

标签 javascript python python-3.x selenium selenium-webdriver

我正在尝试从给定的 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/

相关文章:

python - 将输入转换为字符串和两个 float

python - 如何在 QGraphicsView 中启用平移和缩放

python - 如何在 python 中替换数学方程式中的值?

javascript - 在带有辅助函数的 ejs 中使用 .then 返回 [ object promise ]

javascript - 第三次触发警报,而不是第一次

python - tensorflow.python.framework.errors_impl.NotFoundError : Failed to create a directory: training/export\Servo\temp-b'1576742954'

python - 如何制作带有未定义区域的彩色图?

python - 如何在 Python 3 中切换文本和二进制写入模式?

javascript - 在 HTML/CSS/JS 中创建一个箭头栏

javascript - 使用 ES6 嵌套在 Javascript 中时添加对象