Access to html table retrieved with QNetworkReply

105 Views Asked by At

I want to read the suffix column of the table that I see at http://finance.yahoo.com/exchanges.

I read the page with QNetworkManager. storing data in QNetworkReply *reply. If I read all the page with reply->readAll() I obtain the page, so it's retrieved correctly. I set it inside a QWebPage and then I want to retrieve all tables that are inside.

In order to obtain the table I want to use QWebElement but I'm not be able to read it. I read all tables at the same level of the one that I want but the QWebElementCollection that I obtain is empty.

What I'm doing wrong and that I must do in order to read the table?

void getMarketListFromReply(QNetworkReply *reply) {
    std::cout << "Reading page" << std::endl;
    const QString html(reply->readAll()); // html contain the code
    QWebPage page;
    std::cout << "Setting page." << std::endl;
    page.mainFrame()->setHtml(html);
    std::cout << "Retrieving tables" << std::endl;
    QWebElementCollection tables = page.mainFrame()->documentElement().findAll("html body div.screen div.content table");
    const int size = tables.count();  // size it's 0 :-(
    std::cout << "size: " << size << std::endl;
    for (int i = 0; i < size; i++) {
        std::cout << i << ": " << tables.at(i).toPlainText().toStdString() << std::endl;
    }
}
1

There are 1 best solutions below

4
Orest Hera On

Dot in findAll() selector denotes tag class. However, in your case "screen" and "content" are element ids that can be selected by sharp (#). So, the following selectors should work

.findAll("html body div#screen div#content table");
.findAll("#content table");