如何使用 QWebEngineView 和 QUrl 下载 csv 文件
我正在构建一个程序,它使用 QWebEngineView 和 QUrl 在我的 PyQt5 应用程序(在 Windows 10 上运行)中显示网站。然而,我现在希望能够从同一个网站下载 CSV 文件,但作为一个菜鸟,我似乎不知道如何做。
我熟悉使用 requests
、urllib.request
、urllib3
等来下载文件,但为此,我特别想做它与 QWebEngineView 一起使用,因为用户之前将在 pyqt5 窗口中验证请求。 首先显示网站的代码如下所示:
self.view = QWebEngineView(self)
self.view.load(QUrl(url))
self.view.loadFinished.connect(self._on_load_finished)
self.hbox.addWidget(self.view)
有人对如何实现这一点有任何建议吗?
I'm building a program which uses QWebEngineView
and QUrl
to display a website in my PyQt5 app (running on Windows 10). However, I now want to be able to download a CSV file from the same website, but being a noob I can't seem to figure out how.
I'm familiar with using requests
, urllib.request
, urllib3
, etc. for downloading files, but for this, I specifically want to do it with the QWebEngineView, as the user will have authenticated the request previously in the pyqt5 window.
The code to show the website in the first place goes like this:
self.view = QWebEngineView(self)
self.view.load(QUrl(url))
self.view.loadFinished.connect(self._on_load_finished)
self.hbox.addWidget(self.view)
Does anyone have any suggestion on how this can be achieved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

默认情况下,在 QWebEngineView 中不处理下载,要启用它,您必须使用 QWebEngineProfile 的 downloadRequested 信号,这会传输一个 QWebEngineDownloadItem,如果您希望开始下载,则必须接受该信号:
如果您想直接下载,您可以使用QWebEnginePage的下载方法:
更新:
In QWebEngineView by default the downloads are not handled, to enable it you have to use the downloadRequested signal of QWebEngineProfile, this transports a QWebEngineDownloadItem that you have to accept if you want the download to start:
If you want to make a direct download you can use the download method of QWebEnginePage:
Update: