如何使用 python-webkit2png 同时截取多个屏幕截图?

发布于 2024-08-30 21:13:36 字数 116 浏览 3 评论 0原文

我有来自许多主机的数千个网址需要截图。

我可以从命令行很好地使用该库,但如何将其集成到我的代码中,以便我可以同时截取多个屏幕截图?

我认为这与 xvfb 有关,就像这个问题的答案一样:

I've got thousands of urls from many hosts I need to screenshot.

I can use the lib fine from the command line, but how can I integrate it into my code so I can take multiple screenshots simultaneously?

I think it's something to do with xvfb as with the answer to this question: How to kill headless X server started via Python? but I'm not sure what exactly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白鸥掠海 2024-09-06 21:13:36

这里我使用了一个参数来传递 .txt 的位置,其中包含站点列表(换行符分隔),第二个参数用于传递输出 PNG 文件的位置。

https://gist.github.com/deadstar1/e8d30102afbaefec531d6708f761e104
感谢@paljenczy

here I have used an argument to pass a location of .txt, which contains a list of site(newline delimited), and the second argument for the location of output PNG file.

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

总攻大人 2024-09-06 21:13:36

我使用 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)

I used subprocess to call webkit2png (which was installed through 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's name
    """
    count = 0
    for url, name in list_url_out_name:
        print count
        count += 1
        outpath = outdir + name + '.png'
        scrape_url(url, outpath)
安人多梦 2024-09-06 21:13:36

可能是这样的(未经测试):

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的代码

Probably 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 was shamelessly ripped off from the source code of webkit2png.py.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文