【问题标题】:How to take screenshot of a given url using python如何使用python截取给定url的屏幕截图
【发布时间】:2020-04-06 20:46:26
【问题描述】:

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

我已经经历了以前的解决方案,而我面临的是,

1.pyqt4 - 即使在安装 pyqt4 后也遇到 No module named 'PyQt4.QtWebKit' 错误

2.selenium - 不截取整个页面滚动截图的代码。

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

硒的示例代码:

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

蟒蛇:3.6

预期输出:(任何一个)

1.获取图片的Dataurl

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

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

【问题讨论】:

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


    【解决方案1】:

    您是否尝试过使用 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

    【讨论】:

      猜你喜欢
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      相关资源
      最近更新 更多