PDA

View Full Version : How to parse html with QWebElement?



orkto
23rd December 2014, 16:50
#include <QApplication>
#include <QDebug>
#include <QWebPage>
#include <QWebFrame>
#include <QWebElement>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWebPage frame;
frame.mainFrame()->setHtml("<html><body><p>First Paragraph</p><p>Second Paragraph</p></body></html>");
QWebElement doc = frame.mainFrame()->documentElement();
QWebElement body = doc.firstChild();
qDebug() << body.toPlainText();
QWebElement firstParagraph = body.firstChild();
qDebug() << firstParagraph.toPlainText();
QWebElement secondParagraph = firstParagraph.nextSibling();
qDebug() << secondParagraph.toPlainText();
return a.exec();
}
Output is :
http://i.stack.imgur.com/AwrXq.png
This example is from oficial doc so why this always empty and when i use other exaples it is empty too.

ChrisW67
25th December 2014, 21:33
Print the tagName() of each element and you will likely find that WebKit has provided the missing <head> element and that your "body" variable is pointing there. It has no content, so you see no output. That is what happened with Qt5.4 for me. Try:
QWebElement body = doc.findFirst("body");