我有很多需要截圖的主機的成千上萬的URL。如何使用python-webkit2png同時拍攝很多截圖?
我可以在命令行中使用lib的罰款,但我怎樣才能將它集成到我的代碼,所以我可以同時採取多個屏幕截圖?
我認爲這與xvfb有關,與這個問題的答案一樣:How to kill headless X server started via Python?但我不確定究竟是什麼。
我有很多需要截圖的主機的成千上萬的URL。如何使用python-webkit2png同時拍攝很多截圖?
我可以在命令行中使用lib的罰款,但我怎樣才能將它集成到我的代碼,所以我可以同時採取多個屏幕截圖?
我認爲這與xvfb有關,與這個問題的答案一樣:How to kill headless X server started via Python?但我不確定究竟是什麼。
大概是這樣的(未經測試):
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_())
這是無恥地從source code of webkit2png.py扯掉。
我用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)
謝謝,但我有麻煩的部分是使用Xvfb – oldfellow 2010-04-30 13:09:03