How to parse HTML with C++/Qt?

13,931

Solution 1

EDIT: From the Qt 5.6 release blog post:

With 5.6, Qt WebKit and Qt Quick 1 will no longer be supported and are dropped from the release. The source code for these modules will still be available.

So, as of Qt 5.6 – unless you're willing to compile the sources –, QtWebKit is no longer available. If you're using a Qt release older than 5.6 ot are willing to compile QtWebKit, this might be helpful; otherwise this answer is no longer valid.


It is hard to tell you exactly what needs to be done as your explanation is incomplete about the use case. However, there are two ways of proceeding.

QtWebKit

If you already need any other functionality from that module, this is not going to introduce any further dependencies, and it will be the most convenient for you to use.

You need to get the https://doc.qt.io/archives/qt-5.5/qwebelement.html

That will come once you find the first "span" element in your html:

https://doc.qt.io/archives/qt-5.5/qwebframe.html#findFirstElement

Then, you can simply get the text for that element with the corresponding QWebElement method(s). For instances, you can use this one for getting an attribute value:

https://doc.qt.io/archives/qt-5.5/qwebelement.html#attribute

... but you can also request the attribute names as you can see in the documentation, etc.

This is how you will get the 12345 value:

https://doc.qt.io/archives/qt-5.5/qwebelement.html#toPlainText

XML parser in QtCore

If you do not need webkit for your sotware, and the html data comes in a different way rather than directly from the web for which you would need to use QWebKit, then you are better off using the xml parser available in QtCore. It still might be the case even if you do not have any other dependency from QtWebKit that this additional dependency will not cause any issues in your use case. It is hard to tell based upon your description. For sure, this would be less convenient, albeit not that much, compared to the webkit based solution as that is designed for html.

What you need to avoid is QtXmlPatterns. It is an unmaintained software as of now, and that would introduce an additional dependency for your code either way.

Solution 2

I think QXmlQuery is what you want. I think the code will be like

QXmlQuery query;

query.setQuery(html, QUrl("/body/span[@style='font-size:11p']"));

QString r;
query.evaluateTo(&r);

You can also provide URL directly to the query

query.setQuery(QUrl("http://WWW.testtest.com"), QUrl("/body/span[@style='font-size:11p']"));
Share:
13,931
NPLS
Author by

NPLS

Updated on June 16, 2022

Comments

  • NPLS
    NPLS almost 2 years

    How can i parse the following HTML

    <body>
    <span style="font-size:11px">12345</span>
    <a>Hello<a>
    </body>
    

    I would like to retrive the data "12345" from a "span" with style="font-size:11px" from www.testtest.com, but I only want the that very data, and nothing else.

    How can I accomplish this?

  • László Papp
    László Papp over 10 years
    I think qtxmlpatterns (and hence this recommendation) would be a bit too much for this simple task. QtWebKit is fine with dealing with html, or if someone wishes to avoid even that, then QtCore's xml parser. However, if one deals with html, it is likely that other functionality would be needed from webkit as well.
  • Lol4t0
    Lol4t0 over 10 years
    @LaszloPapp, QtWebkit is a lot heavier than xmlpatterns. QtWebkit is the largest Qt part actually.
  • László Papp
    László Papp over 10 years
    Lol4t0: your answer still does not make sense to me. If he has to use webkit already, that is exactly zero additional dependency. Otherwise he would need to use QtCore. Your answer is wrong either way. I will give a -1 tomorrow because I reached the limit for today. :) Basically you are suggesting an unmaintained extra dependency. That is bad.
  • László Papp
    László Papp over 10 years
    Please re-read it. I wrote if. See also my reply, and the comment before.
  • Romário
    Romário almost 8 years
    QtWebKit was removed from Qt, so this answer is outdated. Is there an alternative to it aside from QtXmlPatterns?
  • Romário
    Romário almost 8 years
    Well, as of Qt 5.6, QtWebKit is no longer there, so this is now the more correct answer.
  • ilotXXI
    ilotXXI almost 8 years
    Maybe casual regular expression is more suitable than whole browser? He just needs some values.