QWebEngineUrlRequestInterceptor 不工作
QWebEngineUrlRequestInterceptor not working
我正在将应用程序从 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)
问题是由使用 MyWebEnginePage
的构造函数引起的,因为您使用构造函数放置了它:
QWebEnginePage::QWebEnginePage(QObject *parent = Q_NULLPTR)
Constructs an empty QWebEnginePage with the parent parent.
代替第二个构造函数:
QWebEnginePage::QWebEnginePage(QWebEngineProfile *profile, QObject
*parent = Q_NULLPTR)
Constructs an empty web engine page in the web engine profile profile
with the parent parent.
If the profile is not the default profile, the caller must ensure that
the profile stays alive for as long as the page does.
This function was introduced in Qt 5.5.
解决方案是传递 2 个参数:配置文件和父级,另外,如果您不打算在构造函数中添加任何内容,则无需覆盖它,如下所示:
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInterceptor
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage, QWebEngineProfile
class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
def interceptRequest(self, info):
# info.setHttpHeader("X-Frame-Options", "ALLOWALL")
print("interceptRequest")
print(info.requestUrl())
class MyWebEnginePage(QWebEnginePage):
def acceptNavigationRequest(self, url, _type, isMainFrame):
print("acceptNavigationRequest")
print(url)
return QWebEnginePage.acceptNavigationRequest(self, url, _type, isMainFrame)
if __name__ == "__main__":
app = QApplication(sys.argv)
browser = QWebEngineView()
interceptor = WebEngineUrlRequestInterceptor()
profile = QWebEngineProfile()
profile.setRequestInterceptor(interceptor)
page = MyWebEnginePage(profile, browser)
page.setUrl(QUrl(""))
browser.setPage(page)
browser.show()
sys.exit(app.exec_())
我正在将应用程序从 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)
问题是由使用 MyWebEnginePage
的构造函数引起的,因为您使用构造函数放置了它:
QWebEnginePage::QWebEnginePage(QObject *parent = Q_NULLPTR)
Constructs an empty QWebEnginePage with the parent parent.
代替第二个构造函数:
QWebEnginePage::QWebEnginePage(QWebEngineProfile *profile, QObject *parent = Q_NULLPTR)
Constructs an empty web engine page in the web engine profile profile with the parent parent.
If the profile is not the default profile, the caller must ensure that the profile stays alive for as long as the page does.
This function was introduced in Qt 5.5.
解决方案是传递 2 个参数:配置文件和父级,另外,如果您不打算在构造函数中添加任何内容,则无需覆盖它,如下所示:
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInterceptor
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage, QWebEngineProfile
class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
def interceptRequest(self, info):
# info.setHttpHeader("X-Frame-Options", "ALLOWALL")
print("interceptRequest")
print(info.requestUrl())
class MyWebEnginePage(QWebEnginePage):
def acceptNavigationRequest(self, url, _type, isMainFrame):
print("acceptNavigationRequest")
print(url)
return QWebEnginePage.acceptNavigationRequest(self, url, _type, isMainFrame)
if __name__ == "__main__":
app = QApplication(sys.argv)
browser = QWebEngineView()
interceptor = WebEngineUrlRequestInterceptor()
profile = QWebEngineProfile()
profile.setRequestInterceptor(interceptor)
page = MyWebEnginePage(profile, browser)
page.setUrl(QUrl(""))
browser.setPage(page)
browser.show()
sys.exit(app.exec_())