在加载网页期间打印出所有请求的 URL [英] print out all the requested URLs during loading a web page
问题描述
在我的项目中,有一些需要在加载网页时通过Python在Chrome Dev Tools中获取某个请求的URL,
In my project, there is some need to get a certain requested URL in Chrome Dev Tools by Python during loading a web page,
我认为通过 Qt WebEngine 获取 URL 是一个很好的解决方案.我首先尝试使用下面的代码在加载网页期间打印出所有请求的 URL,但它不起作用 - 根本没有打印 URL,所以这里有什么问题?有什么好的解决办法吗?
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_())
推荐答案
我在 C++ 中使用 Qt,但在 Python 中应该是类似的,
I use Qt with C++, but in Python should be something similar,
如果你想处理 HTTP/HTTPS url,你可以继承 QWebEnginePage,并重新实现
If you are trying to handle HTTP/HTTPS urls, you could subclass QWebEnginePage, and reimplement
acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool main).
要使用您自己的 QWebEngineUrlRequestInterceptor,您必须指定您正在使用的协议.C++ 中的示例:
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 我不使用 Python,但让我们在 Python 中尝试这样的事情:
Dorry i dont use Python, but lets try something like this in Python:
profile.setRequestInterceptor("https://", WebEngineUrlRequestInterceptor())
请看这篇文章:pyqt5.6 拦截请求不起作用
这篇关于在加载网页期间打印出所有请求的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!