QT += webkitwidgets
divタグで挟まれたテキストを取得場合、
aタグのリンクアドレスとテキストを取得する場合
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mainwindow.h" | |
#include "ui_mainwindow.h" | |
#include <QDebug> | |
#include <QWebElement> | |
#include <QWebFrame> | |
#include <QWebView> | |
MainWindow::MainWindow(QWidget *parent) : | |
QMainWindow(parent), | |
ui(new Ui::MainWindow) | |
{ | |
ui->setupUi(this); | |
QWebPage page; | |
QWebFrame * frame = page.mainFrame(); | |
frame->setHtml("<html><body><div>Hello World!</div><div>DIV2</div><a href=\"http://test.net/\">リンク</a></body></html>"); | |
QWebElementCollection elements = frame->findAllElements("div"); | |
foreach (QWebElement element, elements) | |
{ | |
QString plainText = element.toPlainText(); | |
qDebug() << plainText; | |
} | |
QWebElement document2 = frame->documentElement(); | |
QWebElement firstTextInput = document2.findFirst("a"); | |
QString addr = firstTextInput.attribute("href"); | |
QString text = firstTextInput.toPlainText(); | |
qDebug() << addr << "::" << text; | |
} | |
MainWindow::~MainWindow() | |
{ | |
delete ui; | |
} | |
結果:
"Hello World!"
"DIV2"
"http://test.net/" :: "リンク"
0 件のコメント:
コメントを投稿