简体   繁体   English

如何使用 QWebEngineView 和 QUrl 下载 csv 文件

[英]How to download csv file with QWebEngineView and QUrl

I'm building a program which uses QWebEngineView and QUrl to display a website in my PyQt5 app (running on Windows 10).我正在构建一个程序,该程序使用QWebEngineViewQUrl在我的 PyQt5 应用程序(在 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.但是,我现在希望能够从同一网站下载 CSV 文件,但作为菜鸟,我似乎无法弄清楚如何。

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.我熟悉使用requestsurllib.requesturllib3等来下载文件,但为此,我特别想用 QWebEngineView 来做,因为用户之前已经在 pyqt5 窗口中对请求进行了身份验证。 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?有没有人对如何实现这一目标有任何建议?

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:在 QWebEngineView 中,默认情况下不处理下载,要启用它,您必须使用 QWebEngineProfile 的 downloadRequested 信号,这会传输一个 QWebEngineDownloadItem,如果您希望下载开始,您必须接受它:

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        self.view = QtWebEngineWidgets.QWebEngineView()
        self.view.page().profile().downloadRequested.connect(
            self.on_downloadRequested
        )
        url = "https://domain/your.csv"
        self.view.load(QtCore.QUrl(url))
        hbox = QtWidgets.QHBoxLayout(self)
        hbox.addWidget(self.view)

    @QtCore.pyqtSlot("QWebEngineDownloadItem*")
    def on_downloadRequested(self, download):
        old_path = download.url().path()  # download.path()
        suffix = QtCore.QFileInfo(old_path).suffix()
        path, _ = QtWidgets.QFileDialog.getSaveFileName(
            self, "Save File", old_path, "*." + suffix
        )
        if path:
            download.setPath(path)
            download.accept()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

If you want to make a direct download you can use the download method of QWebEnginePage:如果你想直接下载,你可以使用 QWebEnginePage 的下载方法:

self.view.page().download(QtCore.QUrl("https://domain/your.csv"))

Update:更新:

@QtCore.pyqtSlot("QWebEngineDownloadItem*")
def on_downloadRequested(self, download):
    old_path = download.url().path()  # download.path()
    suffix = QtCore.QFileInfo(old_path).suffix()
    path, _ = QtWidgets.QFileDialog.getSaveFileName(
        self, "Save File", old_path, "*." + suffix
    )
    if path:
        download.setPath(path)
        download.accept()
        download.finished.connect(self.foo)

def foo(self):
    print("finished")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM