如何让PyQt5在返回页面前等待n秒

How to make PyQt5 wait n seconds before returning page

我试图通过将 url 作为参数传递到函数中来使用以下 PyQt5 函数抓取网页。函数 returns 页面数据:

def get_html(url):
    class Render(QWebEngineView):
        def __init__(self, url):
            self.html = None
            self.app = APP.instance()
            QWebEngineView.__init__(self)
            self.loadFinished.connect(self._loadFinished)
            self.load(QUrl(url))

            while self.html is None:
                self.app.processEvents(QEventLoop.ExcludeUserInputEvents | QEventLoop.ExcludeSocketNotifiers | QEventLoop.WaitForMoreEvents)
            self.app.quit()

        def _callable(self, data):
            self.html = data

        def _loadFinished(self, result):
            self.page().toHtml(self._callable)

    return Render(url).html

但是由于对我来说未知的原因,该程序遇到了一个 cloudflare 错误,它需要几秒钟才能正确加载页面。那么如何让函数等待 n 秒以完全加载页面,然后再继续 return 页面数据?

您应该使用 QTimer 而不是直接调用 toHtml:

from PyQt5.QtCore import QTimer, QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEnginePage


APP = QApplication.instance()
if APP is None:
    APP = QApplication([])


class PageHelper(QWebEnginePage):
    def __init__(self, url, timeout=0):
        self.html = ""
        self.timeout = timeout
        app = APP.instance()
        super().__init__()
        self.loadFinished.connect(self.handle_load_finished)

        self.load(QUrl.fromUserInput(url))

        app.exec_()

    def handle_load_finished(self, ok):
        if ok:
            QTimer.singleShot(self.timeout * 1000, self.to_html)
        else:
            QApplication.quit()

    def to_html(self):
        self.toHtml(self._html_callable)

    def _html_callable(self, html):
        self.html = html
        QApplication.quit()


def get_html(url, timeout=0):
    page = PageHelper(url, timeout)
    return page.html


print(get_html(" timeout=5))