[英]Take a screenshot from a website from commandline or with python
我将从这个页面截图: http : //books.google.de/books? id= gikDAAAAMBAJ&pg=PA1&img=1&w=2500或者保存它输出的图像。
但我找不到办法。 使用 wget/curl 我得到一个“不可用错误”以及其他工具,如 webkit2png/wkhtmltoimage/wkhtmltopng。
有没有一种干净的方法可以用 python 或命令行来做到这一点?
最好的祝福!
如果您愿意,可以使用 ghost.py。 https://github.com/jeanphix/Ghost.py
这是一个如何使用它的示例。
from ghost import Ghost
ghost = Ghost(wait_timeout=4)
ghost.open('http://www.google.com')
ghost.capture_to('screen_shot.png')
最后一行将图像保存在当前目录中。
希望这可以帮助
有时您需要额外的 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())
或者您可以查看在 wget 或 curl 中添加 http 标头的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.