从命令行或使用python从网站截取屏幕截图

dan*_*gge 8 python command-line screenshot web

我将从此页面截取屏幕截图:http://books.google.de/books? id = gikDAAAAMBAJ&p = PA1&img = 1&w = 2500或保存其输出的图像.

但我找不到办法.使用wget/curl,我得到一个"不可用的错误",还有其他工具,如webkit2png/wkhtmltoimage/wkhtmltopng.

有没有一个干净的方法来使用python或命令行?

最好的祝福!

Sas*_*ean 14

如果你愿意,你可以使用ghost.py. http://jeanphix.me/Ghost.py/

以下是如何使用它的示例.

from ghost import Ghost
ghost = Ghost(wait_timeout=4)
ghost.open('http://www.google.com')
ghost.capture_to('screen_shot.png')
Run Code Online (Sandbox Code Playgroud)

最后一行将图像保存在当前目录中.

希望这可以帮助

  • 好一个.看起来真的很好,但我不想安装Qt.:/ (4认同)

tde*_*ney 6

有时您需要额外的HTTP标头,例如User-Agent才能使下载工作.在python 2.7中,您可以:

import urllib2
request = urllib2.Request(
    r'http://books.google.de/books?id=gikDAAAAMBAJ&pg=PA1&img=1&w=2500',
    headers={'User-Agent':'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 firefox/2.0.0.11'})
page = urllib2.urlopen(request)

with open('somefile.png','wb') as f:
    f.write(page.read())
Run Code Online (Sandbox Code Playgroud)

或者您可以查看用于在wget或curl中添加http标头的参数.


bil*_*rds 6

我很难让Ghost在无头Centos VM上持续截屏. SeleniumPhantomJS为我工作:

from selenium import webdriver
br = webdriver.PhantomJS()
br.get('http://www.stackoverflow.com')
br.save_screenshot('screenshot.png')
br.quit
Run Code Online (Sandbox Code Playgroud)