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]How to parse html in qt
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]How to parse html in qt

Scheduled Pinned Locked Moved General and Desktop
14 Posts 2 Posters 15.7k 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.
  • T Offline
    T Offline
    ThElitEyeS
    wrote on 3 Jul 2013, 12:01 last edited by
    #1

    As the title said, in QT GUI application.
    should i use regex?
    if yes how i can use this in qt regex?
    @<div\sclass="result-box">(.*)</div></div><div\sclass="ad-page-S1"\sid="DA_03-0-4"></div>@

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 3 Jul 2013, 12:10 last edited by
      #2

      depending on the use case regular expressions are sub-optimal for parsing html.
      What information do you exactly want? Propably the best would be to load the file using QWebPage and get the contents of it using the QWebElement API.

      Also you can read "this":http://qt-project.org/wiki/Handling_HTML.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • T Offline
        T Offline
        ThElitEyeS
        wrote on 3 Jul 2013, 12:54 last edited by
        #3

        i have added webkitwidgets in pro file.
        #include <QWebElement>
        #include <QWebFrame>
        in mainwindow.cpp file
        if i try the following
        @
        QWebFrame *frame;
        frame->setHtml("<html></html>");
        QWebElement parse = frame->documentElement();
        @

        its will crash:The program has unexpectedly finished.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 3 Jul 2013, 12:57 last edited by
          #4

          and what happens when you use QWebPage instead of QWebFrame (as is said)?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • T Offline
            T Offline
            ThElitEyeS
            wrote on 3 Jul 2013, 13:06 last edited by
            #5

            Same thing
            [quote author="raven-worx" date="1372856223"]and what happens when you use QWebPage instead of QWebFrame (as is said)?[/quote]

            @#include <QWebPage>
            QString source(reply->readAll());
            QWebPage *page;
            page->mainFrame()->setHtml(source);@

            The program has unexpectedly finished.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 3 Jul 2013, 13:09 last edited by
              #6

              -can you run your application in your IDE and in debug mode and post the stack trace of the crash.-

              nvm...do this please:
              @
              QWebPage *page = new QWebPage;
              page->mainFrame()->setHtml(source);
              @
              or this
              @
              QWebPage page;
              page.mainFrame()->setHtml(source);
              @

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • T Offline
                T Offline
                ThElitEyeS
                wrote on 3 Jul 2013, 13:42 last edited by
                #7

                2nd one works.
                @ QString source(reply->readAll());
                QWebPage page;
                page.mainFrame()->setHtml(source);
                QWebElement parse = page.mainFrame()->documentElement();
                QWebElement result = parse.findFirst("div[class=result-box]");
                ui->plainTextEdit->setPlainText(result.toPlainText());@
                the last line cause crash
                !http://s16.postimg.org/6dgypgt9h/Screenshot_from_2013_07_03_16_40_27.png(1)!
                !http://s22.postimg.org/j4qwk68wx/Screenshot_from_2013_07_03_16_41_36.png(2)!

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  raven-worx
                  Moderators
                  wrote on 3 Jul 2013, 13:48 last edited by
                  #8

                  when working with QWebElement it's good practice to check if the element is null: QWebElement::isNull() ... please check that.
                  This happens when the search was unsuccessful for example.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    ThElitEyeS
                    wrote on 3 Jul 2013, 13:54 last edited by
                    #9

                    @QMessageBox::information(this, "T", QString::number(result.isNull()));@
                    this breaks the code too

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      raven-worx
                      Moderators
                      wrote on 3 Jul 2013, 14:15 last edited by
                      #10

                      i just tried your code and it is working for me.

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        ThElitEyeS
                        wrote on 3 Jul 2013, 14:19 last edited by
                        #11

                        i tried to put <html></html> and its worked, i think the html source i got who make the crash?

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          raven-worx
                          Moderators
                          wrote on 3 Jul 2013, 14:22 last edited by
                          #12

                          seems so...maybe this is a bug in the Qt implementation.
                          maybe you can find out what exactly causes the crash? You got the full source code from the network reply. You can save the string and use it for testing which part exactly causes the crash.

                          Did you also try loading another url? Does it only occur on a specific webpage?

                          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                          If you have a question please use the forum so others can benefit from the solution in the future

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            ThElitEyeS
                            wrote on 3 Jul 2013, 14:24 last edited by
                            #13

                            i have downloaded the latest release qt 5.1.0 and the crash stopped.

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              ThElitEyeS
                              wrote on 4 Jul 2013, 22:26 last edited by
                              #14

                              thanks for your helping.

                              1 Reply Last reply
                              0

                              1/14

                              3 Jul 2013, 12:01

                              • Login

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