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.
JavaScript
x
2
1
<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>
2
If I simply remove this target atrribute,It works well.
This works
JavaScript
1
2
1
<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>
2
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.
JavaScript
1
83
83
1
import sys
2
from functools import cached_property
3
4
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
5
6
7
class WebView(QtWebEngineWidgets.QWebEngineView):
8
def createWindow(self, type_):
9
if not isinstance(self.window(), Browser):
10
return
11
12
if type_ == QtWebEngineWidgets.QWebEnginePage.WebBrowserTab:
13
return self.window().tab_widget.create_tab()
14
15
16
class TabWidget(QtWidgets.QTabWidget):
17
def __init__(self, parent=None):
18
super().__init__(parent)
19
self.setTabsClosable(True)
20
21
def create_tab(self):
22
view = WebView()
23
24
index = self.addTab(view, "(Untitled)")
25
self.setTabIcon(index, view.icon())
26
view.titleChanged.connect(self.update_title)
27
view.iconChanged.connect(self.update_icon)
28
self.setCurrentWidget(view)
29
return view
30
31
def update_title(self, title):
32
view = self.sender()
33
index = self.indexOf(view)
34
self.setTabText(index, title)
35
36
def update_icon(self, icon):
37
view = self.sender()
38
index = self.indexOf(view)
39
self.setTabIcon(index, icon)
40
41
42
class Browser(QtWidgets.QMainWindow):
43
def __init__(self, parent=None):
44
super().__init__(parent)
45
QtWebEngineWidgets.QWebEngineProfile.defaultProfile().downloadRequested.connect(
46
self.on_downloadRequested, QtCore.Qt.UniqueConnection
47
)
48
49
self.setCentralWidget(self.tab_widget)
50
51
view = self.tab_widget.create_tab()
52
view.load(QtCore.QUrl("https://www.remove.bg"))
53
54
@cached_property
55
def tab_widget(self):
56
return TabWidget()
57
58
def on_downloadRequested(self, download):
59
download.finished.connect(self.download_finished)
60
default_path = download.downloadFileName()
61
suffix = QtCore.QFileInfo(default_path).suffix()
62
path, _ = QtWidgets.QFileDialog.getSaveFileName(
63
self, "Save File", default_path, "*." + suffix
64
)
65
if path:
66
download.setPath(path)
67
download.accept()
68
69
def download_finished(self):
70
self.tab_widget.removeTab(self.tab_widget.currentIndex())
71
72
73
def main():
74
app = QtWidgets.QApplication(sys.argv)
75
76
w = Browser()
77
w.show()
78
sys.exit(app.exec_())
79
80
81
if __name__ == "__main__":
82
main()
83