Python Webkit making web-site screenshots using virtual framebuffer

懵懂的女人 提交于 2019-12-01 09:04:29

问题


The problem is that I need capture web-site screenshots without running X server.

So theoretically it's possible to create a virtual frame buffer and to use it to capture screenshot.

Is there any similar solutions, any advice would be appreciated?

Sultan


回答1:


you can use a combination of Selenium WebDriver and pyvirtualdisplay (which uses xvfb) to run your browser in a virtual display and capture screenshots.

so, the setup you need is:

  • Selenium Python bindings
  • pyvirtualdisplay Python package (depends on xvfb)

On Debian/Ubuntu Linux systems, you can setup everything with:

  • $ sudo apt-get install python-pip xvfb
  • $ sudo pip install selenium

once you have it setup, the following code example should work:

#!/usr/bin/env python

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

browser = webdriver.Firefox()
browser.get('http://www.google.com')
browser.save_screenshot('screenie.png')
browser.quit()

display.stop()

this will:

  • launch a virtual display
  • launch Firefox browser
  • navigate to google.com
  • capture a screenshot
  • close the browser
  • stop the virtual display


来源:https://stackoverflow.com/questions/6572575/python-webkit-making-web-site-screenshots-using-virtual-framebuffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!