In my project, there is some need to get a certain requested URL in Chrome Dev Tools by Python during loading a web page,
I think get the URL by Qt WebEngine is a good solution. I started by trying to use the code below to print out all the requested URLs during loading a web page, but it didn't work - no URL get printed at all , so what's wrong here ? Any good solutions ?
import sys
import os
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWebEngineCore import *
from PyQt5.QtCore import QUrl
class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
def __init__(self, parent=None):
super().__init__(parent)
def interceptRequest(self, info):
print(info.requestUrl())
if __name__ == '__main__':
app = QApplication(sys.argv)
profile = QWebEngineProfile()
profile.setRequestInterceptor(WebEngineUrlRequestInterceptor())
page = QWebEnginePage(profile)
page.setUrl(QUrl(
"http://music.163.com/"))
view = QWebEngineView()
view.setPage(page)
view.resize(1024, 600)
view.show()
sys.exit(app.exec_())