Làm cách nào để "kết xuất" HTML với PyQt5 v5.6 QWebEngineView?Cách "hiển thị" HTML với QWebEngineView của PyQt5
Trước đây tôi đã thực hiện tác vụ với PyQt5 v5.4.1 QWebPage, nhưng nó là suggested để thử QWebEngineView mới hơn.
Dưới đây là thực hiện điều đó (nó thường làm việc như mong đợi, nhưng có xu hướng treo vô thời hạn đối với một số trang web và các tình huống):
def render(source_html):
"""Fully render HTML, JavaScript and all."""
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebKitWidgets import QWebPage
class Render(QWebPage):
def __init__(self, html):
self.html = None
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self._loadFinished)
self.mainFrame().setHtml(html)
self.app.exec_()
def _loadFinished(self, result):
self.html = self.mainFrame().toHtml()
self.app.quit()
return Render(source_html).html
import requests
sample_html = requests.get(dummy_url).text
print(render(sample_html))
gì sau nỗ lực của tôi lúc sử dụng QWebEngineView là. Thứ nhất, quá trình cài đặt và thiết lập của PyQt5 v5.6 trên Ubuntu:
# install PyQt5 v5.6 wheel from PyPI
pip3 install --user pyqt5
# link missing resources
ln -s ../resources/icudtl.dat ../resources/qtwebengine_resources.pak ../resources/qtwebengine_resources_100p.pak ../resources/qtwebengine_resources_200p.pak ../translations/qtwebengine_locales ~/.local/lib/python3.5/site-packages/PyQt5/Qt/libexec/
Bây giờ cho các Python ... Kết quả sau một lỗi segmentation:
def render(source_html):
"""Fully render HTML, JavaScript and all."""
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
class Render(QWebEngineView):
def __init__(self, html):
self.html = None
self.app = QApplication(sys.argv)
QWebEngineView.__init__(self)
self.loadFinished.connect(self._loadFinished)
self.setHtml(html)
self.app.exec_()
def _loadFinished(self, result):
# what's going on here? how can I get the HTML from toHtml?
self.page().toHtml(self.callable)
self.app.quit()
def callable(self, data):
self.html = data
return Render(source_html).html
import requests
sample_html = requests.get(dummy_url).text
print(render(sample_html))
Vấn đề dường như nằm trong cuộc gọi đến không đồng bộ toHtml()
. Có vẻ như nó khá đơn giản, nhưng tôi không biết phải làm gì với nó. Tôi thấy nó là discussed trong ngữ cảnh của C++, nhưng tôi không chắc chắn làm thế nào để dịch này sang Python. Làm thế nào tôi có thể lấy HTML ra?