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. Html parser

Html parser

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 9 Posters 3.8k Views
  • 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.
  • SGaistS SGaist

    Hi,

    If it's only to get the URL contained in the href attribute then using a regex like href="(.*)" with QRegularExpression should do the job.

    S Offline
    S Offline
    satyanarayana143
    wrote on last edited by
    #6

    @SGaist said in Html parser:

    href="(.*)"

    can you give some example how to extract href with regex

    <html>
    <head><title>Index of /satfiles/</title></head>
    <body bgcolor="white">
    <h1>Index of /satfiles/</h1><hr><pre><a href="../">../</a>
    <a href="test1.txt">test1.txt</a> 28-Oct-2018 08:08 14
    <a href="test2.txt">test2.txt</a> 28-Oct-2018 08:09 13
    <a href="yazmifiletest.ts">yazmifiletest.ts</a> 28-Oct-2018 08:09 30
    </pre><hr></body>
    </html>

    jsulmJ K 2 Replies Last reply
    0
    • S satyanarayana143

      @SGaist said in Html parser:

      href="(.*)"

      can you give some example how to extract href with regex

      <html>
      <head><title>Index of /satfiles/</title></head>
      <body bgcolor="white">
      <h1>Index of /satfiles/</h1><hr><pre><a href="../">../</a>
      <a href="test1.txt">test1.txt</a> 28-Oct-2018 08:08 14
      <a href="test2.txt">test2.txt</a> 28-Oct-2018 08:09 13
      <a href="yazmifiletest.ts">yazmifiletest.ts</a> 28-Oct-2018 08:09 30
      </pre><hr></body>
      </html>

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #7

      @satyanarayana143 http://doc.qt.io/qt-5/qregularexpression.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #8

        Hi
        Something like

        QString html = R"(
                <html>
                <head><title>Index of /satfiles/</title></head>
                <body bgcolor="white">
                <h1>Index of /satfiles/</h1><hr><pre><a href="../">../</a>
                <a href="test1.txt">test1.txt</a> 28-Oct-2018 08:08 14
                <a href="test2.txt">test2.txt</a> 28-Oct-2018 08:09 13
                <a href="yazmifiletest.ts">yazmifiletest.ts</a> 28-Oct-2018 08:09 30
                </pre><hr></body>
                </html>)";
        
        QString regex = R"(href="([^"]*))";
        ----
         QRegularExpression re(regex);
          QRegularExpressionMatchIterator i = re.globalMatch(html);
          while (i.hasNext()) {
            QRegularExpressionMatch match = i.next();
            qDebug().noquote() << match.captured();
          }
        
        

        Gets you

        href="../
        href="test1.txt
        href="test2.txt
        href="yazmifiletest.ts
        

        you can use
        https://regexr.com/
        to tweak the expression to match exactly.

        S ManiRonM 2 Replies Last reply
        3
        • S satyanarayana143

          @SGaist said in Html parser:

          href="(.*)"

          can you give some example how to extract href with regex

          <html>
          <head><title>Index of /satfiles/</title></head>
          <body bgcolor="white">
          <h1>Index of /satfiles/</h1><hr><pre><a href="../">../</a>
          <a href="test1.txt">test1.txt</a> 28-Oct-2018 08:08 14
          <a href="test2.txt">test2.txt</a> 28-Oct-2018 08:09 13
          <a href="yazmifiletest.ts">yazmifiletest.ts</a> 28-Oct-2018 08:09 30
          </pre><hr></body>
          </html>

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #9

          @satyanarayana143

          Try out with the QRegularExpression example

          In contrast to the single line version of the QRegExp example you can test short text sections.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          2
          • mrjjM mrjj

            Hi
            Something like

            QString html = R"(
                    <html>
                    <head><title>Index of /satfiles/</title></head>
                    <body bgcolor="white">
                    <h1>Index of /satfiles/</h1><hr><pre><a href="../">../</a>
                    <a href="test1.txt">test1.txt</a> 28-Oct-2018 08:08 14
                    <a href="test2.txt">test2.txt</a> 28-Oct-2018 08:09 13
                    <a href="yazmifiletest.ts">yazmifiletest.ts</a> 28-Oct-2018 08:09 30
                    </pre><hr></body>
                    </html>)";
            
            QString regex = R"(href="([^"]*))";
            ----
             QRegularExpression re(regex);
              QRegularExpressionMatchIterator i = re.globalMatch(html);
              while (i.hasNext()) {
                QRegularExpressionMatch match = i.next();
                qDebug().noquote() << match.captured();
              }
            
            

            Gets you

            href="../
            href="test1.txt
            href="test2.txt
            href="yazmifiletest.ts
            

            you can use
            https://regexr.com/
            to tweak the expression to match exactly.

            S Offline
            S Offline
            satyanarayana143
            wrote on last edited by
            #10

            @mrjj

            thakk you for giving solution i want like this only i have solved my problem.

            1 Reply Last reply
            0
            • mrjjM mrjj

              Hi
              Something like

              QString html = R"(
                      <html>
                      <head><title>Index of /satfiles/</title></head>
                      <body bgcolor="white">
                      <h1>Index of /satfiles/</h1><hr><pre><a href="../">../</a>
                      <a href="test1.txt">test1.txt</a> 28-Oct-2018 08:08 14
                      <a href="test2.txt">test2.txt</a> 28-Oct-2018 08:09 13
                      <a href="yazmifiletest.ts">yazmifiletest.ts</a> 28-Oct-2018 08:09 30
                      </pre><hr></body>
                      </html>)";
              
              QString regex = R"(href="([^"]*))";
              ----
               QRegularExpression re(regex);
                QRegularExpressionMatchIterator i = re.globalMatch(html);
                while (i.hasNext()) {
                  QRegularExpressionMatch match = i.next();
                  qDebug().noquote() << match.captured();
                }
              
              

              Gets you

              href="../
              href="test1.txt
              href="test2.txt
              href="yazmifiletest.ts
              

              you can use
              https://regexr.com/
              to tweak the expression to match exactly.

              ManiRonM Offline
              ManiRonM Offline
              ManiRon
              wrote on last edited by
              #11

              @mrjj said in Html parser:

              R

              sir in this what does R mean for in the QString html

              aha_1980A 1 Reply Last reply
              0
              • ManiRonM ManiRon

                @mrjj said in Html parser:

                R

                sir in this what does R mean for in the QString html

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #12

                @ManiRon: https://solarianprogrammer.com/2011/10/16/cpp-11-raw-strings-literals-tutorial

                Qt has to stay free or it will die.

                ManiRonM 1 Reply Last reply
                1
                • aha_1980A aha_1980

                  @ManiRon: https://solarianprogrammer.com/2011/10/16/cpp-11-raw-strings-literals-tutorial

                  ManiRonM Offline
                  ManiRonM Offline
                  ManiRon
                  wrote on last edited by
                  #13

                  @aha_1980 said in Html parser:

                  https://solarianprogrammer.com/2011/10/16/cpp-11-raw-strings-literals-tutorial

                  error: 'R' was not declared in this scope
                  QString qHtml = R"(<button type="button">Click Me!</button>)";
                  ^

                  mrjjM 1 Reply Last reply
                  0
                  • ManiRonM ManiRon

                    @aha_1980 said in Html parser:

                    https://solarianprogrammer.com/2011/10/16/cpp-11-raw-strings-literals-tutorial

                    error: 'R' was not declared in this scope
                    QString qHtml = R"(<button type="button">Click Me!</button>)";
                    ^

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #14

                    @ManiRon
                    Hi
                    Code seems fine - so it must be compiler version.

                    Is it an older Qt / Compiler ?

                    Its a c++ 11 feature with R (raw strings)

                    ManiRonM 1 Reply Last reply
                    1
                    • mrjjM mrjj

                      @ManiRon
                      Hi
                      Code seems fine - so it must be compiler version.

                      Is it an older Qt / Compiler ?

                      Its a c++ 11 feature with R (raw strings)

                      ManiRonM Offline
                      ManiRonM Offline
                      ManiRon
                      wrote on last edited by
                      #15

                      @mrjj its Qt 5.5

                      mrjjM 1 Reply Last reply
                      0
                      • ManiRonM ManiRon

                        @mrjj its Qt 5.5

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #16

                        @ManiRon
                        Hi
                        Can you try to add
                        CONFIG += c++11
                        in your .pro file
                        then run qmake and rebuild all (from menu) and see if it accepts it then.

                        ManiRonM 2 Replies Last reply
                        0
                        • mrjjM mrjj

                          @ManiRon
                          Hi
                          Can you try to add
                          CONFIG += c++11
                          in your .pro file
                          then run qmake and rebuild all (from menu) and see if it accepts it then.

                          ManiRonM Offline
                          ManiRonM Offline
                          ManiRon
                          wrote on last edited by
                          #17

                          @mrjj said in Html parser:

                          CONFIG += c++11

                          i will check and say sir

                          1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @ManiRon
                            Hi
                            Can you try to add
                            CONFIG += c++11
                            in your .pro file
                            then run qmake and rebuild all (from menu) and see if it accepts it then.

                            ManiRonM Offline
                            ManiRonM Offline
                            ManiRon
                            wrote on last edited by ManiRon
                            #18

                            @mrjj

                            1. It worked sir
                            2. But i am trying to create a button as you can see in the previous code its just displaying the text click me and not the button in the HTML file
                            3. I know that in the list of attribute supported by QT <button> tag is not there whether we can make QT to make work on this
                            mrjjM 1 Reply Last reply
                            0
                            • ManiRonM ManiRon

                              @mrjj

                              1. It worked sir
                              2. But i am trying to create a button as you can see in the previous code its just displaying the text click me and not the button in the HTML file
                              3. I know that in the list of attribute supported by QT <button> tag is not there whether we can make QT to make work on this
                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by mrjj
                              #19

                              @ManiRon
                              Hi
                              Super. so was just c+11 support.
                              The QT widgets cannot draw html Buttons so if you
                              view them in textEdit/Browser no buttons can be shown.

                              I know no easy way to have buttons, but you can try with
                              Qt WebEngine
                              instead and see if it likes it.

                              ManiRonM 1 Reply Last reply
                              1
                              • mrjjM mrjj

                                @ManiRon
                                Hi
                                Super. so was just c+11 support.
                                The QT widgets cannot draw html Buttons so if you
                                view them in textEdit/Browser no buttons can be shown.

                                I know no easy way to have buttons, but you can try with
                                Qt WebEngine
                                instead and see if it likes it.

                                ManiRonM Offline
                                ManiRonM Offline
                                ManiRon
                                wrote on last edited by ManiRon
                                #20

                                @mrjj as i have not worked on it any sample will be available for Qtwebengine which can be help full to understand it

                                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