Unsolved Saving images on QWebEngine
-
This should be one of the most basic things in a web browser but I can't find out how to do it.
I have a custom context menu on my QWebEngine browser and I want to save online images to disk.
I've already seen an example on how to save the entire html page to disk but that's rarely needed.
Can this be done? The browser would first have to recognize all the images on the web page and get their URLs. -
@Harborman Use js(read about runJavaScript method), also read https://stackoverflow.com/a/9321881/6622587
-
@eyllanesc said in Saving images on QWebEngine:
@Harborman Use js(read about runJavaScript method), also read https://stackoverflow.com/a/9321881/6622587
Thanks. This is currently outside my skill range but I'll look into it.
I didn't even know you could mix JS with Python, I'm surprised. -
Just wanted to add for future readers that I came up with a simple workaround:
First you check in your browser's custom context menu if an image was right-clicked:
if self.page().contextMenuData().mediaType() == 1: #test if an image was clicked (img = 1, video = 2, audio = 3) self.menus = QMenu() imgurl = self.page().contextMenuData().mediaUrl() action1 = QAction("Save image to disk", self) self.menus.addAction(action1) self.menus.popup(event.globalPos()) action1.triggered.connect(lambda imgsave: self.saveimg(imgurl))
This sends the image's url to a custom function that saves the image using Python's requests library.
def saveimg(self, imgurl): fileurl = imgurl.toString() #needs to be string response = requests.get(fileurl) filename = fileurl.split("/")[-1] #get filename from url dlfolder = "downloads/" #put your desired folder path here file_path = os.path.join(dlfolder, filename) open(file_path, "wb").write(response.content)