开发者

Qt: can i load and manipulate the web page dom with QWebKit without rendering the page?

开发者 https://www.devze.com 2023-01-14 18:52 出处:网络
is there any way to load url and to ma开发者_开发技巧nipulate the page dom without rendering the page

is there any way to load url and to ma开发者_开发技巧nipulate the page dom without rendering the page i like to do it problematically without showing the page it self in the browser


I believe you should be able to load the web page using QNetworkAccessManager and manipulate its content using QTextDocument; below is a small example. Also you can use QWebPage class without showing the page contents. I also included it into the example below:

void MainWindow::on_pushButton_clicked()
{
    // load web page
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    manager->get(QNetworkRequest(QUrl("http://www.google.com")));
}

void MainWindow::replyFinished(QNetworkReply* reply)
{
    QByteArray content = reply->readAll();

    // process network reply using QTextDocument 
    QTextDocument page;
    page.setHtml(content);
    for (QTextBlock block = page.begin(); block != page.end(); block = block.next())
    {
        // do smth here
        qDebug() << block.text();
    }

    // process network reply using QWebPage    
    QWebPage webPage;
    webPage.mainFrame()->setHtml(content);

    QWebElement document = webPage.mainFrame()->documentElement();
    QWebElementCollection elements = document.findAll("element_name");

    foreach (QWebElement element, elements)
    {
        // do smth here
        qDebug() << element.toPlainText();
    }
}

hope this helps, regards

0

精彩评论

暂无评论...
验证码 换一张
取 消