Skip to content
Advertisement

I have a small problem with PyQt5 WebEngineView and html of url: “https://www.remove.bg”

I have Enabled downloads in pyqtwebengineview and it works well. However,I have some problems with the site:”https://www.remove.bg”

So I downloaded The html content of that site and the problem is an anchor tag.

<a href="https://o.remove.bg/downloads/df97f73e-975d-4ff6-9558-d0ad4b486f79/back-removebg-preview.png"target="_blank" rel="noopener" ondragstart="return false;" class="btn btn-primary" style="min-width: 190px;">Download</a>

If I simply remove this target atrribute,It works well.
This works

<a href="https://o.remove.bg/downloads/df97f73e-975d-4ff6-9558-d0ad4b486f79/back-removebg-preview.png"rel="noopener" ondragstart="return false;" class="btn btn-primary" style="min-width: 190px;">Download</a>

difference is just target atrribute.What to do. How to make this site work correctly.

Advertisement

Answer

The redirection link has an attribute of “_blank” that according to the protocol must open a new tab so in a previous answer I explained what the implementation should be, I will omit that part and will only show the code where I remove the new tab when the download is finished.

import sys
from functools import cached_property

from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets


class WebView(QtWebEngineWidgets.QWebEngineView):
    def createWindow(self, type_):
        if not isinstance(self.window(), Browser):
            return

        if type_ == QtWebEngineWidgets.QWebEnginePage.WebBrowserTab:
            return self.window().tab_widget.create_tab()


class TabWidget(QtWidgets.QTabWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setTabsClosable(True)

    def create_tab(self):
        view = WebView()

        index = self.addTab(view, "(Untitled)")
        self.setTabIcon(index, view.icon())
        view.titleChanged.connect(self.update_title)
        view.iconChanged.connect(self.update_icon)
        self.setCurrentWidget(view)
        return view

    def update_title(self, title):
        view = self.sender()
        index = self.indexOf(view)
        self.setTabText(index, title)

    def update_icon(self, icon):
        view = self.sender()
        index = self.indexOf(view)
        self.setTabIcon(index, icon)


class Browser(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        QtWebEngineWidgets.QWebEngineProfile.defaultProfile().downloadRequested.connect(
            self.on_downloadRequested, QtCore.Qt.UniqueConnection
        )

        self.setCentralWidget(self.tab_widget)

        view = self.tab_widget.create_tab()
        view.load(QtCore.QUrl("https://www.remove.bg"))

    @cached_property
    def tab_widget(self):
        return TabWidget()

    def on_downloadRequested(self, download):
        download.finished.connect(self.download_finished)
        default_path = download.downloadFileName()
        suffix = QtCore.QFileInfo(default_path).suffix()
        path, _ = QtWidgets.QFileDialog.getSaveFileName(
            self, "Save File", default_path, "*." + suffix
        )
        if path:
            download.setPath(path)
            download.accept()

    def download_finished(self):
        self.tab_widget.removeTab(self.tab_widget.currentIndex())


def main():
    app = QtWidgets.QApplication(sys.argv)

    w = Browser()
    w.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement