Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用QWebElement解析HTML,如何提取图像?_Python_Qt_Webkit_Qtwebkit_Qwebelement - Fatal编程技术网

Python 使用QWebElement解析HTML,如何提取图像?

Python 使用QWebElement解析HTML,如何提取图像?,python,qt,webkit,qtwebkit,qwebelement,Python,Qt,Webkit,Qtwebkit,Qwebelement,我很难使用QWebElement。作为练习,我想从页面中捕获“Google”徽标。图像在中,但我不知道如何提取它。在下面的代码中如何使用“doc”QWebElement?(“CSS选择器”对我来说是晦涩难懂的行话)。 多谢各位 from PyQt4.QtGui import QApplication from PyQt4.QtWebKit import QWebView from PyQt4.QtCore import QUrl app = QApplication([]) view = QW

我很难使用QWebElement。作为练习,我想从页面中捕获“Google”徽标。图像在
中,但我不知道如何提取它。在下面的代码中如何使用“doc”QWebElement?(“CSS选择器”对我来说是晦涩难懂的行话)。 多谢各位

from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebView
from PyQt4.QtCore import QUrl

app = QApplication([])
view = QWebView()
view.load(QUrl("http://google.com"))
view.show()
doc = view.page().currentFrame().documentElement()   # run this after 'loadFinished'

您只需提取
属性的
src
属性

imgTags = doc.findAll("img")
imgRightTag = QWebElement()

# Find the right <img/> tag and put it in imgRightTag

imgURL = "http://www.google.com" + imgRightTag.attribute("src")
image = QImage(imgURL)
imgTags=doc.findAll(“img”)
imgRightTag=QWebElement()
#找到正确的以获取“Google”徽标的URL,请执行以下操作:

elem = doc.findFirst("div#hplogo")
qstring = elem.attribute('style')
regexp = QRegExp("^(.*:)?url\((.*)\)")
if regexp.indexIn(qstring) > -1:
    imageURL = regexp.capturedTexts()[-1]
它返回
imageURL=“/images/srpr/logo1w.png”
。在这种情况下,有必要使用regexp,因为URL是字符串的一部分。要获取图像并将其显示在标签上,请执行以下操作:

request = QNetworkRequest(QUrl("http://www.google.com/images/srpr/logo1w.png"))
reply = view.page().networkAccessManager().get(request)
byte_array = reply.readAll()
image = QImage()
image.loadFromData(byte_array)
label = QLabel()
label.setPixmap(QPixmap(image))
label.show()

很抱歉这么糟糕,但是复制粘贴代码似乎不起作用。我试图解释它,并设法从
imgURL=doc.findFirst(“img”).attribute(“src”)
中获取一个QString,其值等于
“/images/icons/product/chrome-48.png”
。但我在下一步努力。。。如何访问此字符串应该指向的图像?QImage().load(imgURL)返回False。在基本URL前面加上前缀。”“应该行。我编辑了我的答案。它似乎也不适用于预先设置的链(我不想再下载刚刚下载的图像)。