I want to save to a file all the http requests (get, post, ...) that a QWebEngineView sends during a navigation session (like I would do with Firefox: inspect > network > save to HAR file).
Below are two codes I tried. In theses scripts I simply try to print the requests.
The first script displays correctly the view but fails to intercept the requests.
# SCRIPT 1
import sys
from PyQt6.QtCore import *
from PyQt6.QtWebEngineCore import *
from PyQt6.QtWidgets import *
from PyQt6.QtWebEngineWidgets import *
class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
def interceptRequest(self, info: QWebEngineUrlRequestInfo):
print()
print("interceptRequest")
print(info.requestUrl())
print(info.requestMethod())
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
interceptor = WebEngineUrlRequestInterceptor()
self.profile = QWebEngineProfile.defaultProfile()
self.profile.setUrlRequestInterceptor(interceptor)
print(self.profile)
self.browser = QWebEngineView(self.profile, parent=self)
self.setCentralWidget(self.browser)
self.browser.setUrl(QUrl("https://google.com"))
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
app.exec()
The second script catches and display the requests but the window is not rendered.
# SCRIPT 2
import sys
from PyQt6.QtCore import *
from PyQt6.QtWebEngineCore import *
from PyQt6.QtWebEngineWidgets import *
from PyQt6.QtWidgets import *
class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
def interceptRequest(self, info: QWebEngineUrlRequestInfo):
print()
print("interceptRequest")
print(info.requestUrl())
print(info.requestMethod())
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.setUrlRequestInterceptor(interceptor)
page = MyWebEnginePage(profile)
page.setUrl(QUrl("https://google.com"))
browser.setPage(page)
browser.showMaximized()
app.exec()
Can somebody help patching either of these scripts?