【问题标题】:pyqt5.6 interceptRequest doesn't workpyqt5.6拦截请求不起作用
【发布时间】: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


【解决方案1】:

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

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

到:

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

就是这样。

【讨论】:

  • 这是一个与Python相关的问题,更多讨论在这里pyqt.riverbankcomputing.narkive.com/N9NK1wyq/…
  • 这个问题似乎仍然存在于 PyQt 5.10 中。我想知道为什么我自己的尝试确实失败了,但您给出了正确的解决方案:确保请求拦截器不会过早收集垃圾。
猜你喜欢
  • 2018-02-02
  • 2021-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 2019-07-14
  • 2010-11-21
相关资源
最近更新 更多