【发布时间】:2016-06-06 13:28:25
【问题描述】:
我想通过子类 QWebEngineUrlRequestInterceptor 拦截一个 url 请求:
class RequestInterceptor(QWebEngineUrlRequestInterceptor):
def interceptRequest(self,info):
print('#################interceptRequest')
print(info.requestUrl(),info.firstPartyUrl(),info.NavigationType,info.resourceType(),info.requestMethod())
if info.requestUrl().endswith("/jquery.js"):
info.redirect('/jqueryTest.js')
app = QApplication([])
p = QWebEnginePage()
v = QWebEngineView()
v.setPage(p)
p.profile().setRequestInterceptor(RequestInterceptor())
c.registerObject('bridge', p)
url = "http://127.0.0.1:8000/test.html?t=5"
v.setUrl(QUrl(url))
v.show()
app.exec_()
当我运行代码时,拦截器不起作用!
希望有人帮帮我,谢谢!
PS: 可能是python垃圾回收造成的。所以我通过修改代码将拦截器存储在变量中
p.profile().setRequestInterceptor(RequestInterceptor())
到
interceptor = RequestInterceptor()
p.profile().setRequestInterceptor(interceptor )
仅此而已。
【问题讨论】:
-
“不起作用”究竟是什么意思?您提议的更改有帮助吗?
标签: qt interceptor pyqt5 qwebpage