原文:https://blog.csdn.net/tymatlab/article/details/78647543
PyQt5 渲染动态网页
示例代码:
# -*- coding: UTF-8 -*-
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView
import lxml.html
class Render(QWebEngineView): # 子类Render继承父类QWebEngineView
def __init__(self, url):
self.html = ”
self.app = QApplication(sys.argv)
QWebEngineView.__init__(self) # 子类构造函数继承父类,这种写法python2和3通用,还可以是super().__init__()
self.loadFinished.connect(self._loadFinished)
self.load(QUrl(url))
self.app.exec_()
def _loadFinished(self):
self.page().toHtml(self.callable)
def callable(self, data):
self.html = data
self.app.quit()
if __name__ == ‘__main__’:
url = ‘http://example.webscraping.com/places/default/dynamic’
r = Render(url)
result = r.html
tree = lxml.html.fromstring(result)
a = tree.cssselect(‘#result’)[0].text_content()
print(a)
注意:
1.PyQt版本之间的区别
版本 | 用法 |
---|---|
PyQt4 | from PyQt4.QtWebKit import QWebPage, QWebView |
PyQt5 | from PyQt5.QtWebKitWidgets import QWebPage, QWebView |
PyQt5.6+ |
|
可参考:
- http://www.widlabs.com/article/no-module-named-pyqt5-qtwebkitwidgets
2.PyQt4
中的mainFrame
在PyQt5
中已废弃
版本 | 用法 |
---|---|
PyQt4 | html = webview.page().mainFrame().toHtml() |
PyQt5 | self.page().toHtml(self.callable) |
可参考: