Qt read data from website

1.8k views Asked by At

what library do I have to use to get data from website.

For example I have such a case:

<html>
 <body>
  <div class = "MyClass"> 
    <div class = "MyClass2"> </div>
  </div>
 </body>
</html>

And how do I get the name of the second class(Myclass2), what method do I have to use? I just wanna mention that in c# it was very easy here is more complicated in my opinion.

I was using QWebElement class but I haven't found proper method.

Greetings

2

There are 2 answers

1
Nejat On

You can use QWebPage class which provides an object to view and edit web documents. Using QWebElement you can access to DOM elements in a QWebFrame :

QWebPage page;
page.mainFrame()->load(url);

QWebFrame * frame = page.mainFrame();
QWebElement doc = frame->documentElement();
QWebElement body = doc.firstChild();
QWebElement first = body.firstChild();
QWebElement second = body.firstChild();

QString str = second.attribute("class");
0
Matthias On

You can use QXmlStreamReader to access your Dokument too.

Example loop:

QXmlStreamReader xml;
...
while (!xml.atEnd()) {
      xml.readNext();
      ... // do processing
}
if (xml.hasError()) {
      ... // do error handling
}

A helpful Example: QXmlStream Bookmarks Example