2

QWebElement の使用に苦労しています。演習として、ページhttp://www.google.comから「Google」のロゴをキャプチャしたいと思います。画像は に<div id="hplogo" ...>ありますが、抽出方法がわかりません。次のコードで「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'
4

2 に答える 2

3

「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"。その場合、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()
于 2013-01-19T21:22:02.443 に答える
2

src画像を含む HTML タグの属性を抽出し、その属性を<img/>使用して画像を作成するだけ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)
于 2013-01-05T15:37:07.607 に答える