如何通过QNetworkAccessManager找出下载图像的确切URL

时间:2019-05-01 05:17:10

标签: python beautifulsoup pyside qnetworkaccessmanager qnetworkreply

我正在使用QNetworkAccessManager下载图像。

第一次,我必须指定图像的网址。

但这与众不同。

例如,我从Wikipedia Top Page Japanese的图片中获取。

有一些正在上传的图片。

如果我复制文本和图像,然后将其粘贴到编辑器中, html文本包含网址。

即使我将其传递到QNetworkRequest,也无法下载图像。

在我深入研究URL并到达Wikipedia Commons中的图片文件之后, 如果我将网址传递到QNetworkRequest,则可以下载图像。

我已经知道网址必须准确,但是我不知道如何区分网址。

您知道如何每次都以编程方式下载图像而不会出错吗?

有什么办法可以提前知道确切的网址吗?

我对网络不了解。

我经常接受UnknownContentError

如何使用示例代码

Ctrl + C 复制文本和图像(请包含图像。) Ctrl + V 粘贴HTML并下载图片

Q 直接下载图像。我只能以此下载。

示例代码

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

1 个答案:

答案 0 :(得分:1)

您遇到以下错误:

  • 您应该使用的网址位于children img标签的src属性中。
  • 您仅需创建一个QNetworkAccessManager,否则新的QNetworkAccessManager将消除前一个。
  • 您必须使用QUrl::fromUserInput(),因为它可以处理尝试多种配置的网址,例如,在这种情况下,它可以转换以下网址:
https://ja.wikipedia.org/wiki/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB:Oguri_Cap_in_Yushun_Stallion_station.jpg

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