如何使用 QWebEngineView 和 QUrl 下载 csv 文件 [英] How to download csv file with QWebEngineView and QUrl

查看:64
本文介绍了如何使用 QWebEngineView 和 QUrl 下载 csv 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个程序,该程序使用 QWebEngineViewQUrl 在我的 PyQt5 应用程序(在 Windows 10 上运行)中显示一个网站.但是,我现在希望能够从同一网站下载 CSV 文件,但作为菜鸟,我似乎不知道如何下载.

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.

我熟悉使用requestsurllib.requesturllib3等下载文件,但为此,我专门想要用 QWebEngineView 来做它,因为用户将在 pyqt5 窗口之前对请求进行身份验证.首先显示网站的代码是这样的:

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?

推荐答案

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

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:

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

如果要直接下载可以使用QWebEnginePage的下载方式:

If you want to make a direct download you can use the download method of QWebEnginePage:

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

更新:

@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")

这篇关于如何使用 QWebEngineView 和 QUrl 下载 csv 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆