python--动态网页渲染pyqt5

原文: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版本之间的区别

版本用法
PyQt4from PyQt4.QtWebKit import QWebPage, QWebView
PyQt5from PyQt5.QtWebKitWidgets import QWebPage, QWebView
PyQt5.6+

from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView

可参考:

 

2.PyQt4中的mainFramePyQt5中已废弃

版本用法
PyQt4html = webview.page().mainFrame().toHtml()
PyQt5self.page().toHtml(self.callable)

 

可参考:

    原文作者:秋华
    原文地址: https://www.cnblogs.com/qiu-hua/p/9100880.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞