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. Parse HTML in Qt 5.7

Parse HTML in Qt 5.7

Scheduled Pinned Locked Moved Unsolved General and Desktop
htmlparsing
12 Posts 5 Posters 6.0k 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.
  • A Offline
    A Offline
    AliReza Beytari
    wrote on last edited by
    #1

    Hi.
    I have a QString which contain HTML Source Code. Like this :

    <!DOCTYPE html>
    <html>
    <head>
    	<meta charset="utf-8">
    	<title>Hello World</title>
    </head>
    <body>
    	<input type="hidden" name="token" value="fc5e038d38a57032085441e7fe7010b0">
    </body>
    </html>
    

    So I want to get the value of hidden input. (with a function like find("input[name=token]").
    In Qt 5.4 I used to use QWebPage, QWebFrame, QWebElement, etc. But now I can't use them.

    How can I parse HTML codes ?!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What about QString with a QRegularExpression ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        The entire Qt XML framework, including QXmlStreamReader classes can read HTML, you just need to set them to ignore errors due to root item not being unique

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        4
        • SGaistS SGaist

          Hi,

          What about QString with a QRegularExpression ?

          A Offline
          A Offline
          AliReza Beytari
          wrote on last edited by
          #4

          @SGaist But I don't want to use regular expressions!!

          @VRonin "QXmlStreamReader" doesn't have any "find" function !!

          beeckscheB Joel BodenmannJ 2 Replies Last reply
          0
          • A AliReza Beytari

            @SGaist But I don't want to use regular expressions!!

            @VRonin "QXmlStreamReader" doesn't have any "find" function !!

            beeckscheB Offline
            beeckscheB Offline
            beecksche
            wrote on last edited by beecksche
            #5

            @AliReza-Beytari

            You can subclass the QXmlStreamReader and implement your find - function. Like the code in the detailed desciption of the function

            QString SearchFunction()
            {
              QXmlStreamReader xml;
              ...
              while (!xml.atEnd()) {
                    xml.readNext();
                    // your search!
            
              }
              if (xml.hasError()) {
                    ... // do error handling
              }
            
            }
            
            
            A 1 Reply Last reply
            0
            • beeckscheB beecksche

              @AliReza-Beytari

              You can subclass the QXmlStreamReader and implement your find - function. Like the code in the detailed desciption of the function

              QString SearchFunction()
              {
                QXmlStreamReader xml;
                ...
                while (!xml.atEnd()) {
                      xml.readNext();
                      // your search!
              
                }
                if (xml.hasError()) {
                      ... // do error handling
                }
              
              }
              
              
              A Offline
              A Offline
              AliReza Beytari
              wrote on last edited by
              #6

              @beecksche There is no way to use WebKit ?!

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Sure there is: build the module from sources.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • A AliReza Beytari

                  @SGaist But I don't want to use regular expressions!!

                  @VRonin "QXmlStreamReader" doesn't have any "find" function !!

                  Joel BodenmannJ Offline
                  Joel BodenmannJ Offline
                  Joel Bodenmann
                  wrote on last edited by Joel Bodenmann
                  #8

                  @AliReza-Beytari said:

                  @SGaist But I don't want to use regular expressions!!

                  May we ask why? Is this because you don't know how to build the regex pattern? If so, people are happy to help, there are also more than enough tools out there that will assist you with that :)
                  Whenever you have to find stuff in strings, regex is usually the way to go (unless it's some more primitive pattern - which isn't the case here).

                  @AliReza-Beytari said:

                  There is no way to use WebKit ?!

                  QtWebKit was replaced by QtWebEngine: https://wiki.qt.io/Porting_from_QtWebKit_to_QtWebEngine

                  Industrial process automation software: https://simulton.com
                  Embedded Graphics & GUI library: https://ugfx.io

                  A 1 Reply Last reply
                  1
                  • Joel BodenmannJ Joel Bodenmann

                    @AliReza-Beytari said:

                    @SGaist But I don't want to use regular expressions!!

                    May we ask why? Is this because you don't know how to build the regex pattern? If so, people are happy to help, there are also more than enough tools out there that will assist you with that :)
                    Whenever you have to find stuff in strings, regex is usually the way to go (unless it's some more primitive pattern - which isn't the case here).

                    @AliReza-Beytari said:

                    There is no way to use WebKit ?!

                    QtWebKit was replaced by QtWebEngine: https://wiki.qt.io/Porting_from_QtWebKit_to_QtWebEngine

                    A Offline
                    A Offline
                    AliReza Beytari
                    wrote on last edited by
                    #9

                    @Joel-Bodenmann I don't like to use regular expressions because Qt had some functions to parse HTML.

                    I know that QtWebKit was replaced by QtWebEngine, but I can't find any QWebEngineFrame or QWebEnginElement. (I found QWebEnginePage)

                    What is the replacement of QWebFrame and QWebElement ?!

                    Joel BodenmannJ 1 Reply Last reply
                    0
                    • A AliReza Beytari

                      @Joel-Bodenmann I don't like to use regular expressions because Qt had some functions to parse HTML.

                      I know that QtWebKit was replaced by QtWebEngine, but I can't find any QWebEngineFrame or QWebEnginElement. (I found QWebEnginePage)

                      What is the replacement of QWebFrame and QWebElement ?!

                      Joel BodenmannJ Offline
                      Joel BodenmannJ Offline
                      Joel Bodenmann
                      wrote on last edited by
                      #10

                      The article I linked explains this:

                      QWebFrame has been merged into QWebEnginePage
                      It is not possible to access sub-frames. Methods of the main QWebFrame are now available directly through the QWebEnginePage itself.
                      

                      It also contains corresponding example code.

                      Industrial process automation software: https://simulton.com
                      Embedded Graphics & GUI library: https://ugfx.io

                      A 1 Reply Last reply
                      1
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Out of curiosity, are you using QtWebkit only to grab that token ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • Joel BodenmannJ Joel Bodenmann

                          The article I linked explains this:

                          QWebFrame has been merged into QWebEnginePage
                          It is not possible to access sub-frames. Methods of the main QWebFrame are now available directly through the QWebEnginePage itself.
                          

                          It also contains corresponding example code.

                          A Offline
                          A Offline
                          AliReza Beytari
                          wrote on last edited by
                          #12

                          @Joel-Bodenmann So I can just use "runJavaScript" function ?!

                          @SGaist No, that was just an example !! :D

                          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