我正在将应用程序从 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)