首页  >  文章  >  后端开发  >  如何在 Qt 中使用 QWebPage 高效检索多个 URL?

如何在 Qt 中使用 QWebPage 高效检索多个 URL?

DDD
DDD原创
2024-10-27 11:42:30576浏览

How to Efficiently Retrieve Multiple URLs Using QWebPage in Qt?

使用 QWebPage 检索多个 URL

在此场景中,您尝试使用 Qt 的 QWebPage 来呈现动态更新的页面。但是,您在尝试渲染第二个页面时经常遇到崩溃。

问题分析

问题出在您的方法上。您正在为每个 URL 获取初始化一个新的 QApplication 和 QWebPage。相反,建议维护单个 QApplication 和 QWebPage,使用信号和自定义处理来处理同一实例中的多个 URL。

建议的解决方案

WebPage 类

以下是 PyQt5 和 PyQt4 的自定义 WebPage 类:

PyQt5 WebPage

<code class="python">from PyQt5.QtCore import pyqtSignal, QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEnginePage

class WebPage(QWebEnginePage):
    htmlReady = pyqtSignal(str, str)

    def __init__(self, verbose=False):
        super().__init__()
        self._verbose = verbose
        self.loadFinished.connect(self.handleLoadFinished)

    def process(self, urls):
        self._urls = iter(urls)
        self.fetchNext()

    def fetchNext(self):
        try:
            url = next(self._urls)
        except StopIteration:
            return False
        else:
            self.load(QUrl(url))
        return True

    def processCurrentPage(self, html):
        self.htmlReady.emit(html, self.url().toString())
        if not self self.fetchNext():
            QApplication.instance().quit()

    def handleLoadFinished(self):
        self.toHtml(self.processCurrentPage)

    def javaScriptConsoleMessage(self, *args, **kwargs):
        if self._verbose:
            super().javaScriptConsoleMessage(*args, **kwargs)</code>

PyQt4 WebPage

<code class="python">from PyQt4.QtCore import pyqtSignal, QUrl
from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebPage

class WebPage(QWebPage):
    htmlReady = pyqtSignal(str, str)

    def __init__(self, verbose=False):
        super(WebPage, self).__init__()
        self._verbose = verbose
        self.mainFrame().loadFinished.connect(self.handleLoadFinished)

    def process(self, urls):
        self._urls = iter(urls)
        self.fetchNext()

    def fetchNext(self):
        try: 
            url = next(self._urls)
        except StopIteration:
            return False
        else:
            self.mainFram().load(QUrl(url))
        return True

    def processCurrentPage(self):
        self.htmlReady.emit(self.mainFrame().toHtml(), self.mainFrame().url().toString())
        if not self.fetchNext():
            QApplication.instance().quit()

    def javaScripConsoleMessage(self ,* args, **kwargs):
        if self._verbose:
            super(WebPage, self).javaScriptConsoleMessage(*args, **kwargs)</code>

用法

以下是如何使用这些 WebPage 类的示例:

<code class="python">from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication

# PyQt5
url_list = ['https://example.com', 'https://example2.com']
app = QApplication(sys.argv)
webpage = WebPage(verbose=True)
webpage.htmlReady.connect(my_html_processor)
webpage.process(url_list)
sys.exit(app.exec_())

# PyQt4
from PyQt4.QtCore import QUrl
from PyQt4.QtGui import QApplication
url_list = ['https://example.com', 'https://example2.com']
app = QApplication(sys.argv)
webpage = WebPage(verbose=True)
webpage.htmlReady.connect(my_html_processor)
webpage.process(url_list)
sys.exit(app.exec_())</code>

在此代码中,my_html_processor 是一个可以自定义的函数,用于处理已处理的 HTML 和每个页面的 URL 信息。

通过实施此方法,您可以防止以前遇到的崩溃和随机行为,从而实现更稳定、更高效的网页抓取工作流程。

以上是如何在 Qt 中使用 QWebPage 高效检索多个 URL?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn