PyQt4 QWebPage设置了Qtimer以额外的时间渲染Javascript

时间:2018-07-05 17:06:26

标签: python python-3.x pyqt pyqt4 qtwebkit

我正试图增加额外的时间来渲染Java脚本,我知道我需要为此使用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'))

1 个答案:

答案 0 :(得分:0)

如果要在页面加载后给它额外的时间,则必须在QTimer插槽中使用finished_loading,则不必创建QTimer的实例,就可以使用静态方法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'))