python - QWebEngineUrlRequestInterceptor 不工作
问题描述
我正在将应用程序从 PyQt4 迁移到 PyQt5。
我试图覆盖请求拦截器,但这由于某种奇怪的原因不起作用,这没有被拾起。我正在使用 PyQt==5.10.0
class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
def __init__(self, parent=None):
super().__init__(parent)
def interceptRequest(self, info):
# info.setHttpHeader("X-Frame-Options", "ALLOWALL")
print("test")
print(info.requestUrl())
class MyWebEnginePage(QWebEnginePage):
# adblocker = Filter(open('easylist.txt', encoding="utf8"))
def __init__(self, parent=None):
super().__init__(parent)
def acceptNavigationRequest(self, url, _type, isMainFrame):
# urlString = url.toString()
# resp = False
# resp = WebPage.adblocker.match(url.toString())
#
# if resp:
# print("Blocking url --- "+url.toString())
# return False
# else:
# print("TYPE", _type)
# return True
print(url)
return QWebEnginePage.acceptNavigationRequest(self, url, _type, isMainFrame)
这就是我加载浏览器的方式
# init browser
browser = QWebEngineView()
# init profile
profile = QWebEngineProfile()
# add interceptor to profile
interceptor = WebEngineUrlRequestInterceptor()
profile.setRequestInterceptor(interceptor)
# init page setting profile
page = MyWebEnginePage(profile)
page.setUrl(qurl)
browser.setPage(page)
解决方案
问题是由使用的构造函数引起的MyWebEnginePage
,因为您使用构造函数放置了它:
QWebEnginePage::QWebEnginePage(QObject *parent = Q_NULLPTR)
构造一个带有 parent parent 的空 QWebEnginePage。
而不是第二个构造函数:
QWebEnginePage::QWebEnginePage(QWebEngineProfile *profile, QObject *parent = Q_NULLPTR)
在具有父级的 Web 引擎配置文件配置文件中构造一个空的 Web 引擎页面。
如果配置文件不是默认配置文件,则调用者必须确保配置文件与页面一样长时间保持活动状态。
这个函数是在 Qt 5.5 中引入的。
解决方案是传递 2 个参数:配置文件和父级,如果您不打算在构造函数中添加任何内容,则无需覆盖它,如下所示:
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInterceptor
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage, QWebEngineProfile
class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
def interceptRequest(self, info):
# info.setHttpHeader("X-Frame-Options", "ALLOWALL")
print("interceptRequest")
print(info.requestUrl())
class MyWebEnginePage(QWebEnginePage):
def acceptNavigationRequest(self, url, _type, isMainFrame):
print("acceptNavigationRequest")
print(url)
return QWebEnginePage.acceptNavigationRequest(self, url, _type, isMainFrame)
if __name__ == "__main__":
app = QApplication(sys.argv)
browser = QWebEngineView()
interceptor = WebEngineUrlRequestInterceptor()
profile = QWebEngineProfile()
profile.setRequestInterceptor(interceptor)
page = MyWebEnginePage(profile, browser)
page.setUrl(QUrl("https://stackoverflow.com/questions/50786186/qwebengineurlrequestinterceptor-not-working"))
browser.setPage(page)
browser.show()
sys.exit(app.exec_())
推荐阅读
- scala - SBT 无法解决没有 IVY(仅 POM)的插件的依赖关系
- android - 是否有任何第三方库或 Android 提供的任何组件来显示像 fb feed 这样的图像?
- python - 使用另一个 numpy 数组元素作为索引的矢量化更新 numpy 数组
- c# - Bot 框架 Directline - 更改 Bot 名称 运行时间
- java - java.net.SocketException:在 AWS Lambda 中异步添加 Keen 事件时不允许操作(选择/轮询失败)
- javascript - React 本机弹出菜单中 MenuOption 的自定义样式
- r - ggplot 在 scale_x_discrete 中的中断问题
- python - 如何在 Jupyter 笔记本中添加断点?
- r - Googleway 的按位置排名选项有什么作用?
- javascript - Jquery滚动到选项卡模块并使用锚链接打开选项卡