Python中无头QtWebKit浏览器中多个网页的屏幕截图

时间:2012-08-10 14:06:05

标签: python qt pyqt4 qtwebkit

我正在尝试渲染多个网页并截取它们的截图,但我只能在渲染一个网页时才能使用它,因为当我在多个网页上尝试它时,程序将停止在其轨道中并永久挂起或只是不对图像,css做任何事情,并将提取网站的文本并将其放在一个长文本块中。通常发生的事情就是它会挂起。

我用于在内存中呈现网页的代码是:

class Render(QWebPage):
    def __init__(self, url):
        self.app = QApplication(sys.argv)
        QWebPage.__init__(self)

        # Settings
        s = self.settings()
        #s.setAttribute(QWebSettings.AutoLoadImages, False)
        s.setAttribute(QWebSettings.JavascriptCanOpenWindows, False)
        s.setAttribute(QWebSettings.PluginsEnabled, True)

        self.loadFinished.connect(self._loadFinished)
        self.mainFrame().load(QUrl(url))
        #self.mainFrame().setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff)
        self.mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
        self.app.exec_()

    def _loadFinished(self, result):
        self.frame = self.mainFrame()
        size = self.frame.contentsSize()
        size.setWidth(1366)
        self.setViewportSize(size)
        self.app.quit()

以下是我保存图片的方式:

def run(url):
    os.chdir("output")

    r = Render(url)
    image = QImage(r.viewportSize(), QImage.Format_ARGB32)
    painter = QPainter(image)
    r.frame.render(painter)
    painter.end()
    fp = "%s.png" % os_safe_name(url)
    image.save(fp)
    os.chdir("..")

任何人都知道为什么会这样吗?

2 个答案:

答案 0 :(得分:4)

就像Luke的回答中所描述的那样,我为了避免为每个QApplication

创建一个Render实例而改变了事情。

不是最整洁,但对我有用:

import re
import sys
import time

# Tested with PySide 1.0.9, changing imports to PyQt should work identically
from PySide.QtCore import Qt, QUrl
from PySide.QtGui import QApplication, QImage, QPainter
from PySide.QtWebKit import QWebPage, QWebSettings


def os_safe_name(url):
    url = re.sub("[^a-zA-Z0-9_-]+", "_", url)
    url = re.sub("_{2,}", "_", url)
    return url


class Render(QWebPage):
    def __init__(self, url):
        QWebPage.__init__(self)

        self.url = url
        self.finished = False

        # Settings
        s = self.settings()
        #s.setAttribute(QWebSettings.AutoLoadImages, False)
        s.setAttribute(QWebSettings.JavascriptCanOpenWindows, False)
        s.setAttribute(QWebSettings.PluginsEnabled, True)

        #self.mainFrame().setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff)
        self.mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)

        # When page is loaded, callback saves image to file
        self.loadFinished.connect(self._loadFinished)

        self.mainFrame().load(QUrl(url))

    def _loadFinished(self, result):
        frame = self.mainFrame()
        size = frame.contentsSize()
        size.setWidth(1366)
        self.setViewportSize(size)

        image = QImage(self.viewportSize(), QImage.Format_ARGB32)

        painter = QPainter(image)
        frame.render(painter)
        painter.end()

        self.filepath = "output/%s.png" % os_safe_name(self.url)
        image.save(self.filepath)

        self.finished = True


def run(url, app = None):
    if app is None:
        app = QApplication(sys.argv)

    r = Render(url)

    while not r.finished:
        app.processEvents()
        time.sleep(0.01)

    return r.filepath


if __name__ == '__main__':
    app = QApplication(sys.argv)

    print run("http://stackoverflow.com", app=app)
    print run("http://google.com", app=app)

答案 1 :(得分:1)

我认为您正在创建Render类的多个实例。如果是这种情况,那么您很可能遇到问题,因为您创建了多个QApplication实例。而是创建一个QApplication并在所有Render实例之间共享它。

您可能还需要停止使用app.quit(),因为您希望QApplication继续运行。此外,由于app.exec_()在调用quit()之前不会退出,因此您需要创建自己的事件循环。像这样:

while not self.finished:
    self.app.processEvents()
    time.sleep(0.01)