pyqt5.6拦截请求不';行不通
我想通过子类QWebEngineUrlRequestInterceptor截取另一个url请求:pyqt5.6拦截请求不';行不通,qt,interceptor,pyqt5,qwebpage,Qt,Interceptor,Pyqt5,Qwebpage,我想通过子类QWebEngineUrlRequestInterceptor截取另一个url请求: class RequestInterceptor(QWebEngineUrlRequestInterceptor): def interceptRequest(self,info): print('#################interceptRequest') print(info.requestUrl(),info.firstPartyUrl()
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_()
当我运行代码时,拦截器不工作希望有人能给我帮助,谢谢 附言: 可能是python垃圾收集造成的。因此,我通过修改代码将拦截器存储在Variable中
p.profile().setRequestInterceptor(RequestInterceptor())
到
仅此而已。可能是python垃圾收集造成的。因此,我通过修改代码将拦截器存储在变量中:
p.profile().setRequestInterceptor(RequestInterceptor())
致:
仅此而已。什么是“不起作用”的确切含义?您提出的更改有帮助吗?这是一个与Python相关的问题,这里的更多讨论这个问题似乎仍然存在于PyQt 5.10中。我想知道为什么我自己的尝试失败了,但您给出了正确的解决方案:确保请求拦截器不会过早地收集垃圾。
p.profile().setRequestInterceptor(RequestInterceptor())
interceptor = RequestInterceptor()
p.profile().setRequestInterceptor(interceptor )