PyQt4 QWebPage 设置 Qtimer 用于额外时间渲染 Javascript

PyQt4 QWebPage set the Qtimer for extra time rendering Javascript

我正在尝试添加额外的渲染时间Javascript,我知道我需要为此使用 Qtimer,但我现在不知道如何以正确的方式编码。

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

class Client(QWebPage):
     def __init__(self, url):
         self.app = QApplication(sys.argv)
         QWebPage.__init__(self)
         self.loadFinished.connect(self.finished_loading)
         self.userAgentForUrl(url)
         self.timer = QTimer()
         self.timer.singleShot(15000, self.finished_loading)
         self.mainFrame().load(QUrl(url))
         self.app.exec_()

     def userAgentForUrl(self, url):
         return 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0'

     def finished_loading(self,url):
         self.app.quit()

url=#URL
client_response = Client(url)
source = client_response.mainFrame().toHtml()
print(source.encode('utf-8'))

如果你想在页面加载后给它额外的时间那么你必须在finished_loading插槽中使用QTimer,没有必要创建[=11的实例=], 用静态方法 singleShot():

就够了
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

class Client(QWebPage):
    def __init__(self, url):
        self.app = QApplication(sys.argv)
        QWebPage.__init__(self)
        self.loadFinished.connect(self.finished_loading)
        self.userAgentForUrl(url)
        self.mainFrame().load(QUrl(url))
        self.app.exec_()

    def userAgentForUrl(self, url):
        return 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0'

    def finished_loading(self,url):
        QTimer.singleShot(15*1000, self.app.quit)

url=#URL
client_response = Client(url)
source = client_response.mainFrame().toHtml()
print(source.encode('utf-8'))