How do I use python-webkit2png to take multiple screenshots at the same time?

I have thousands of URLs from many hosts that I need for screenshots.

I can use lib fine from the command line, but how can I integrate it into my code to take multiple screenshots at the same time?

I think it has to do with xvfb as an answer to this question: How do I kill a headless X server running through Python? but I'm not sure what exactly.

+2


a source to share


3 answers


Perhaps something like this (untested):

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_())

      



This has been shamelessly ripped off of the webkit2png.py source code .

0


a source


I used subprocess

to call webkit2png

(which was installed via python-webkit2png

), it worked fine.



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 name
    """
    count = 0
    for url, name in list_url_out_name:
        print count
        count += 1
        outpath = outdir + name + '.png'
        scrape_url(url, outpath)

      

0


a source


here I used an argument to pass the location of the .txt that contains the list of sites (with a newline delimiter), and a second argument for the location of the output PNG file.

https://gist.github.com/deadstar1/e8d30102afbaefec531d6708f761e104 thanks to @paljenczy

0


a source







All Articles