我有来自许多需要截图的主机的数千个网址。
我可以从命令行很好地使用 lib,但是如何将它集成到我的代码中以便我可以同时拍摄多个屏幕截图?
我认为这与 xvfb 有关,就像这个问题的答案一样:如何杀死通过 Python 启动的无头 X 服务器?但我不确定到底是什么。
我有来自许多需要截图的主机的数千个网址。
我可以从命令行很好地使用 lib,但是如何将它集成到我的代码中以便我可以同时拍摄多个屏幕截图?
我认为这与 xvfb 有关,就像这个问题的答案一样:如何杀死通过 Python 启动的无头 X 服务器?但我不确定到底是什么。
在这里,我使用了一个参数来传递 .txt 的位置,其中包含站点列表(换行符分隔),第二个参数用于输出 PNG 文件的位置。
https://gist.github.com/deadstar1/e8d30102afbaefec531d6708f761e104 感谢@paljenczy
我曾经subprocess
打电话webkit2png
(通过安装python-webkit2png
),它工作正常。
def scrape_url(url, outpath):
"""
Requires webkit2png to be on the path
"""
subprocess.call(["webkit2png", "-o", outpath, "-g", "1000", "1260",
"-t", "30", url])
def scrape_list_urls(list_url_out_name, outdir):
"""
list_url_out_name is a list of tuples: (url, name)
where name.png will be the image's name
"""
count = 0
for url, name in list_url_out_name:
print count
count += 1
outpath = outdir + name + '.png'
scrape_url(url, outpath)
可能是这样的(未经测试):
from webkit2png import WebkitRenderer, init_qtgui
from PyQt4.QtCore import QTimer
def renderer_func():
renderer = WebkitRenderer()
renderer.width = 800
renderer.height = 600
renderer.timeout = 10
renderer.wait = 1
renderer.format = "png"
renderer.grabWholeWindow = False
outfile = open("stackoverflow.png", "w")
renderer.render_to_file(url="http://stackoverflow.com", file=outfile)
outfile.close()
app = init_qtgui()
QTimer.singleShot(0, renderer_func)
sys.exit(app.exec_())
这是从webkit2png.py 的源代码中无耻地扯掉的。