【问题标题】:PyQt4 QWebPage set the Qtimer for extra time rendering JavascriptPyQt4 QWebPage 设置 Qtimer 用于额外时间渲染 Javascript
【发布时间】:2018-07-05 17:06:25
【问题描述】:

我正在尝试增加渲染 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'))

【问题讨论】:

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


    【解决方案1】:

    如果你想在页面加载后给它额外的时间,那么你必须在finished_loading插槽中使用QTimer,不需要创建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'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-17
      • 2014-03-13
      • 2011-07-05
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多