- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 QWebEnginePage 对象中获取 html 代码。根据 Qt 引用,QWebEnginePage 对象的 'toHtml' 是异步方法,如下所示。
Asynchronous method to retrieve the page's content as HTML, enclosed in HTML and BODY tags. Upon successful completion, resultCallback is called with the page's content.
class MainWindow(QWidget):
html = None
...
...
def store_html(self, data):
self.html = data
def get_html(self):
current_page = self.web_view.page()
current_page.toHtml(self.store_html)
# I want to wait until the 'store_html' method is finished
# but the 'toHtml' is called asynchronously, return None when try to return self.html value like below.
return self.html
...
...
最佳答案
获得该行为的一种简单方法是使用 QEventLoop()
.此类的对象阻止了 exec_()
之后的代码从被执行,这并不意味着 GUI 不继续工作。
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
class Widget(QWidget):
toHtmlFinished = pyqtSignal()
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
self.setLayout(QVBoxLayout())
self.web_view = QWebEngineView(self)
self.web_view.load(QUrl("http://doc.qt.io/qt-5/qeventloop.html"))
btn = QPushButton("Get HTML", self)
self.layout().addWidget(self.web_view)
self.layout().addWidget(btn)
btn.clicked.connect(self.get_html)
self.html = ""
def store_html(self, html):
self.html = html
self.toHtmlFinished.emit()
def get_html(self):
current_page = self.web_view.page()
current_page.toHtml(self.store_html)
loop = QEventLoop()
self.toHtmlFinished.connect(loop.quit)
loop.exec_()
print(self.html)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
注:相同的方法适用于 PySide2。
关于python - 有没有办法同步调用方法 'toHtml' ,它是 QWebEnginePage 的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47067050/
我们正在尝试将一些应用程序从 Qt 4 移植到 Qt 5.4。 Qt 5.4 有一个新的网络引擎。我们曾经把QWebView和QWebPage的背景做成透明的: view = new QWebView
我有这部分工作,但我面临几个困难: 1) 看来 QWebEnginePage 需要一个 QWebEngineView。 (请参阅此处的 setView() 方法: https://code.woboq
我使用QWebEnginePage.printToPdf方法来打印Qt 5.7中QWebEnginePage渲染的网页。但该方法仅呈现网络的一部分。 代码如下: webEngineView->page
在 Qt 的 (现已弃用) QWebPage 类中,有一个信号 contentsChanged() 每当网页的 html 内容被用户编辑页面或以编程方式更改的页面更改时调用。在较新的 QtWebEng
我在将 QWebEnginePage 连接到 fullScreenRequested 时遇到问题,我正在尝试以下方式,但它给出了错误 main.cpp:58: error: expected prim
我正在使用 QT WebEngine 框架来显示网页。我在页面加载时将 JavaScript 注入(inject)到页面中,并希望允许 JavaScript 能够访问 QT 对象。显然,要做到这一点,
我有一个使用 QWebChannel 启动 Webview 的 Qt 应用程序。 在这些 View 中,我让 JavaScript 执行一些操作,以便根据屏幕大小定位窗口/调整窗口大小。 screen
我有一个 QWebEnginePage 实例,它加载了一些 url(例如 https://www.google.com) 我希望此页面显示在两个 QWebEngineView 中,但看起来 Qt 不允
我正在尝试从 QWebEnginePage 对象中获取 html 代码。根据 Qt 引用,QWebEnginePage 对象的 'toHtml' 是异步方法,如下所示。 Asynchronous me
我正在尝试开发一个可持续的网络抓取脚本,以从网站获取所有产品的列表。产品类别链接位于网页的下拉(或可扩展)元素中。在提取 html 并使用 Beautiful Soup 将其转换为文本之前,我使用 P
本来想解决this problem , 但正如我测试过 QWebEnginePage::runJavaScript() 的行为一样,我发现我什至无法使用带有以下代码的 QWebEnginePage::
我需要从 QWebEnginePage 中检索一些 html。我在文档中找到了方法 toHtml但它总是返回一个空字符串。我试过 toPlainText 它有效,但这不是我需要的。 MyClass::
我在 PyQt5 代码中遇到错误。谁能帮我。 import sys from PyQt5.QtWidgets import QApplication from PyQt5.QtCore import
我正在尝试使用 print() 从 QWebEngineView 打印 html 文档方法和 QPrinter此方法所需的实例。 我遇到的问题是,在生成的 PDF 中,我无法选择任何可用的文本。 PD
我是一名优秀的程序员,十分优秀!