[SOLVED] Parse HTML with QWebElement
-
Hi. I'm trying to extract "16.20" from this html code:
@<table width=100% cellpadding=0 cellspacing=0 border=0>
<tr>
<td><span class=bul>EUR</span></td>
<td><b>16.20<sup >▲</sup></b></td>
<td align=center width=10% >;</td>
<td>15.91<sup >▼</sup> - 16.50<sup >▲</sup></td></tr>
<tr>@But I can not generate appropriate reg. exp. I have stopped on this variant:
@QRegExp exp = QRegExp("<td><b>[1-9]{2,2}.[1-9]{2,2}");@ -
@#include <QApplication>
#include <QDebug>#include <QWebPage>
#include <QWebFrame>
#include <QWebElement>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString html="<table width=100% cellpadding=0 cellspacing=0 border=0>"
"<tr>"
"<td><span class=bul>EUR</span></td>"
"<td><b>16.20<sup >▲</sup></b></td>"
"<td align=center width=10% >;</td>"
"<td>15.91<sup >▼</sup> - 16.50<sup >▲</sup></td></tr>"
"</table>";
QWebPage webPage;
webPage.mainFrame()->setHtml(html);
qDebug() << webPage.mainFrame()->documentElement().toPlainText();
QWebElement tableElement = webPage.mainFrame()->documentElement().findFirst("table");
qDebug() << tableElement.toPlainText();
QWebElementCollection allTDs = tableElement.firstChild().findAll("td");
qDebug() << allTDs.at(1).toPlainText();
QWebElement b = allTDs.at(1).findFirst("b");
QString final = b.toPlainText(); final.chop(1);qDebug() << final << final.count(); return a.exec();
}
@in your .pro file:
@QT += webkitwidgets@
-
Thanks a lot! But this approach works only with the piece of html. When I load a full web-page this code:
@qDebug() << webPage.mainFrame()->documentElement().toPlainText();@write me words from the site instead of pure html code. How can I fix it?
-
Apparently the above code works only for the piece you provided.
It should work fine in any context if you replace
@QWebElement tableElement = webPage.mainFrame()->documentElement().findFirst("table");@
with a QWebElementCollection search for "table", and you pick the specific table you want.
If you understand the code in my previous post, then you shouldn't have any problem to do it in any web page with any html in it.
Obviously, this will not work with any html. For example, my code assumes that the needed value is inside the "b" of the second "td" of the first "tr" of a "table".
Change accordingly.
-
Thank you so much for your precious help;)