4

我正在尝试渲染多个网页并截取它们的屏幕截图,但我只能在渲染一个网页时让它工作,因为当我在多个网页上尝试它时,程序要么停在它的轨道上并永远挂起,要么就是不做任何事情带有图像、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("..")

有谁知道为什么会这样?

4

2 回答 2

4

就像卢克的回答中所描述的那样,我把事情打乱了,以避免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)
于 2012-08-11T16:13:07.800 回答
1

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

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

while not self.finished:
    self.app.processEvents()
    time.sleep(0.01)
于 2012-08-11T15:26:08.810 回答