Parse HTML in Qt 5.7
-
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 ?!
-
Hi,
What about QString with a QRegularExpression ?
-
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
-
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 } }
-
@beecksche There is no way to use WebKit ?!
-
Sure there is: build the module from sources.
-
@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 byQtWebEngine
: https://wiki.qt.io/Porting_from_QtWebKit_to_QtWebEngine -
@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 ?!
-
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.
-
Out of curiosity, are you using QtWebkit only to grab that token ?
-
@Joel-Bodenmann So I can just use "runJavaScript" function ?!
@SGaist No, that was just an example !! :D