Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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']"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top