我正试图增加额外的时间来渲染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'))
答案 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'))