Search code examples
pythonbeautifulsouppysideqnetworkaccessmanagerqnetworkreply

How to find out the exact url for downloading images by QNetworkAccessManager


I'm using QNetworkAccessManager for downloading images.

At the first time, I must specify the url of images.

But it is different by means.

For example, I get from an image from Wikipedia Top Page Japanese.

There are some uploading images.

If I copy the texts and images , and then paste them into my editor, the html text contains the url.

Even if I pass it into the QNetworkRequest, I cannot download the image.

After I dig into the url and arrive at the image file in Wikipedia Commons, If I pass the url into the QNetworkRequest,I can download images.

I have already understood the url must be exact,but I don't know how to differenciate them.

Do you know how to download programmatically the images every time without mistaking?

Is there any way to know the exact url in advance?

I have no much knowledge about network.

I often accept UnknownContentError

How to use for sample code

Ctrl+C copy texts & images (please contain image.) Ctrl+V paste html & download image

Qdownload image directly. I can download by this only.

Sample Code

from PySide import QtCore, QtGui, QtNetwork
from bs4 import BeautifulSoup
import sys, os, re
class Widget(QtGui.QTextEdit):
    def __init__(self, parent=None):
        super(Widget, self).__init__()   

    def keyPressEvent(self, event):
        if event.modifiers() == QtCore.Qt.KeyboardModifier.ControlModifier and event.key() == QtCore.Qt.Key_C:
            tc = self.textCursor()                       
            mimedata = self.createMimeDataFromSelection()
            clipboard = QtGui.QApplication.clipboard()
            clipboard.setMimeData(mimedata)
            start = tc.selectionStart()
            end = tc.selectionEnd()                   
            char = tc.charFormat()
            char.clearBackground()
            tc.setCharFormat(char)             
            return 
        elif event.modifiers() == QtCore.Qt.KeyboardModifier.ControlModifier and event.key() == QtCore.Qt.Key_V:
            clipboard = QtGui.QApplication.clipboard()

            html = clipboard.mimeData().html()                      
            pous = BeautifulSoup(html,"lxml")
            image_items  = pous.find_all('a', href=re.compile('(\.jpg|\.png|\.svg|\.gif)$'))
            image_urls = [item.get('href') for item in image_items]      
            for i, url in enumerate(image_urls):                    
                #indirect url  https://ja.wikipedia.org/wiki/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB:Oguri_Cap_in_Yushun_Stallion_station.jpg
                print(url)
                request = QtNetwork.QNetworkRequest(QtCore.QUrl(url))       
                self.nam = QtNetwork.QNetworkAccessManager() 
                self.nam.finished[QtNetwork.QNetworkReply].connect(self.doReply)
                self.nam.get(request)
        if event.key() == QtCore.Qt.Key_Q:
            self.doRequest()
    def doRequest(self):        
        # direct url & exact url
        url = "https://upload.wikimedia.org/wikipedia/commons/c/c3/Oguri_Cap_in_Yushun_Stallion_station.jpg"
        req = QtNetwork.QNetworkRequest(QtCore.QUrl(url))    
        self.nam = QtNetwork.QNetworkAccessManager()     
        self.nam.finished[QtNetwork.QNetworkReply].connect(self.doReply)       
        self.nam.get(req)    
    def doReply(self, reply):
        er = reply.error()
        if er == QtNetwork.QNetworkReply.NoError:    
            data = reply.readAll()            
            self.saveImage(data)                    
        else:
            print( "Error occured: ", er)
            print(  reply.errorString())
    def saveImage(self, data):
        try:           
            im = QtGui.QImageWriter()            
            im.setFileName(os.path.join(os.getcwd(),"wiki_topimage.png"))           
            imd = QtGui.QImage()
            imd_bool = imd.loadFromData(data.data())
            im.write(imd)
        except Exception as e:
            print(e)

def main():
    QtCore.QCoreApplication.addLibraryPath(os.path.join(os.getcwd(),"plugins"))  
    try:
        QtGui.QApplication([])

    except Exception as e:
        print("error", e)
    w = Widget()
    w.show()
    sys.exit(QtGui.QApplication.exec_())
if __name__ == "__main__":
    main()

Solution

  • You have the following errors:

    • The url you should use is in the src attribute of the children img tag.
    • You only have to create a QNetworkAccessManager since otherwise the new QNetworkAccessManager will eliminate the previous one.
    • You must use QUrl::fromUserInput() since it handles the urls trying several configurations, for example in this case it could convert url of:
    https://ja.wikipedia.org/wiki/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB:Oguri_Cap_in_Yushun_Stallion_station.jpg
    

    to

    https://ja.wikipedia.org/wiki/ファイル:Oguri_Cap_in_Yushun_Stallion_station.jpg
    
    import os
    import sys
    import re
    from PySide import QtCore, QtGui, QtNetwork
    from bs4 import BeautifulSoup
    
    
    class Widget(QtGui.QTextEdit):
        def __init__(self, parent=None):
            super(Widget, self).__init__(parent)
            self.nam = QtNetwork.QNetworkAccessManager(self)
            self.nam.finished[QtNetwork.QNetworkReply].connect(self.doReply)
    
        def keyPressEvent(self, event):
            if event.matches(QtGui.QKeySequence.Copy):
                tc = self.textCursor()
                mimedata = self.createMimeDataFromSelection()
                clipboard = QtGui.QApplication.clipboard()
                clipboard.setMimeData(mimedata)
                start = tc.selectionStart()
                end = tc.selectionEnd()
                char = tc.charFormat()
                char.clearBackground()
                tc.setCharFormat(char)
                return
            elif event.matches(QtGui.QKeySequence.Paste):
                clipboard = QtGui.QApplication.clipboard()
    
                html = clipboard.mimeData().html()
                pous = BeautifulSoup(html, "lxml")
                image_items = pous.find_all(
                    "a", href=re.compile(r"(\.jpg|\.png|\.svg|\.gif)$")
                )
                for i, item in enumerate(image_items):
                    url = item.find("img", recursive=False).get("src")
                    print(i, url)
                    request = QtNetwork.QNetworkRequest(
                        QtCore.QUrl.fromUserInput(url)
                    )
                    self.nam.get(request)
    
        @QtCore.Slot(QtNetwork.QNetworkReply)
        def doReply(self, reply):
            er = reply.error()
            if er == QtNetwork.QNetworkReply.NoError:
                print("ok", reply.url())
                name = reply.url().path().split("/")[-1]
                filename = os.path.join(os.getcwd(), name)
                file = QtCore.QFile(filename)
                if file.open(QtCore.QIODevice.WriteOnly):
                    file.write(reply.readAll())
            else:
                print("Error occured: ", er, reply.url())
            reply.deleteLater()
    
    
    def main():
        QtCore.QCoreApplication.addLibraryPath(os.path.join(os.getcwd(), "plugins"))
        app = QtGui.QApplication.instance()
        if app is None:
            app = QtGui.QApplication(sys.argv)
        w = Widget()
        w.show()
        sys.exit(QtGui.QApplication.exec_())
    
    
    if __name__ == "__main__":
        main()