Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Parse HTML with QWebElement

[SOLVED] Parse HTML with QWebElement

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Giperboloid
    wrote on last edited by
    #1

    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}");@

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexandros
      wrote on last edited by
      #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&#40;&#41;;
      

      }
      @

      in your .pro file:

      @QT += webkitwidgets@

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Giperboloid
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alexandros
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Giperboloid
            wrote on last edited by
            #5

            Thank you so much for your precious help;)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved