Python 如何获取QWebEngineView中加载的页面的html
我正在尝试获取PyQT5 QWebEngineView中加载的页面的HTML。下面是一个简单的例子:Python 如何获取QWebEngineView中加载的页面的html,python,python-3.x,pyqt,pyqt5,qwebengineview,Python,Python 3.x,Pyqt,Pyqt5,Qwebengineview,我正在尝试获取PyQT5 QWebEngineView中加载的页面的HTML。下面是一个简单的例子: import sys from PyQt5.QtCore import * from PyQt5.QtWebEngineWidgets import * from PyQt5.QtWidgets import * def callback_function(html): print(html) def on_load_finished(): web.page().run
import sys
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWidgets import *
def callback_function(html):
print(html)
def on_load_finished():
web.page().runJavaScript("document.getElementsByTagName('html')[0]", callback_function)
app = QApplication(sys.argv)
web = QWebEngineView()
web.load(QUrl("https://stackoverflow.com"))
web.show()
web.loadFinished.connect(on_load_finished)
sys.exit(app.exec_())
我希望能够从runJavaScript()调用返回html,但回调函数中得到一个空白
< >我的代码中有什么错误,有什么可供选择的网页的HTML?< /p> < p>使用我的书面C++,并将解决方案翻译为Python:
导入系统
从PyQt5.QtCore导入QUrl
从PyQt5.QtWebEngineWidgets导入QWebEngineView
从PyQt5.QtWidgets导入QApplication
def回调函数(html):
打印(html)
def on_load_finished():
web.page().runJavaScript(“document.documentElement.outerHTML”,回调函数)
app=QApplication(sys.argv)
web=QWebEngineView()
web.load(QUrl(“https://stackoverflow.com"))
web.show()
web.resize(640480)
web.loadFinished.connect(加载完成时)
sys.exit(app.exec_())
更新: 您案例中的问题是getElementsByTagName()返回js元素列表,并且该元素无法导出到python,您应该做的是获取innerHTML:
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
def callback_function(html):
print(html)
def on_load_finished():
web.page().runJavaScript(
"document.getElementsByTagName('html')[0].innerHTML", callback_function
)
# or document.getElementsByTagName('html')[0].outerHTML
app = QApplication(sys.argv)
web = QWebEngineView()
web.load(QUrl("https://stackoverflow.com"))
web.show()
web.resize(640, 480)
web.loadFinished.connect(on_load_finished)
sys.exit(app.exec_())
选择?可能使用PySide2。您的代码只需更改import@Pa_您可以共享一个示例的链接吗?@Pa_;PySide2不是一个选项,例如它没有实现很多函数,IMHO PyQt5的bug比PySide2少。有没有一种方法没有回调函数,但可以访问原始html?