print out all the requested URLs during loading a

2019-09-12 03:53发布

问题:

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_())

回答1:

I use Qt with C++, but in Python should be something similar,

If you are trying to handle HTTP/HTTPS urls, you could subclass QWebEnginePage, and reimplement

acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool main).

To use your own QWebEngineUrlRequestInterceptor you have to specify the protocol you are using. Example in C++:

const QString EXAMPLE_SCHEMA_HANDLER = "example://" /* http://, https://, mail:// ....   */;
const QWebEngineUrlSchemeHandler* installed =  QWebEngineProfile::defaultProfile()->urlSchemeHandler(EXAMPLE_SCHEMA_HANDLER);
        if (!installed)
            profile()->installUrlSchemeHandler(EXAMPLE_SCHEMA_HANDLER, new WebAppUrlSchemeHandler(this));

Dorry i dont use Python, but lets try something like this in Python:

profile.setRequestInterceptor("https://", WebEngineUrlRequestInterceptor())

See this post: pyqt5.6 interceptRequest doesn't work