pyqt5.6拦截请求不起作用

问题描述 投票:0回答:2

我想通过子类 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垃圾回收导致的。所以我通过修改代码将拦截器存放在varible中

p.profile().setRequestInterceptor(RequestInterceptor())

interceptor = RequestInterceptor()
p.profile().setRequestInterceptor(interceptor )

仅此而已。

qt interceptor pyqt5 qwebpage
2个回答
2
投票

可能是Python垃圾回收造成的。所以我通过修改代码将拦截器存储在变量中:

p.profile().setRequestInterceptor(RequestInterceptor())

至:

interceptor = RequestInterceptor()
p.profile().setRequestInterceptor(interceptor )

仅此而已。


0
投票

将其作为属性添加到窗口对象对我有用,实际上可能是像这里提到的一些垃圾收集问题。这里的另一个解决方案对我不起作用。

self.interceptor = WebEngineUrlRequestInterceptor()
profile = QWebEngineProfile().defaultProfile()
profile.setUrlRequestInterceptor(self.interceptor)
© www.soinside.com 2019 - 2024. All rights reserved.