c++ - Qt html解析没有找到任何标签

标签 c++ html qt html-parsing

我正在编写一个爬行应用程序。我得到的代码中的某处:

//normally the HTML is obtained from web with QNetworkAccessManager & QNetworkReply:
//QString htmlCode = this->reply->readAll();  

//exemplary test HTML
QString htmlCode =QString("<html><body><a href=\'foo.bar\'>test1</a><h2>test2<h2><a href=\"bar.foo  \">test3</a></body></html>"); 
QWebPage page;
QWebFrame * frame = page.mainFrame(); //->setHtml(htmlCode);
frame->setHtml(htmlCode);
QWebElement document = frame->documentElement();
QWebElementCollection links = document.findAll("a");
foreach (QWebElement e, links) {
   qDebug() << "exemplary link:" << e.toPlainText();
}

我确实意识到,这里有数以百万计的关于在 qt 中解析 html 的主题,但我不知道这里出了什么问题......

最佳答案

嗯...我不确定 setHtml() 是否完全同步工作,即我认为框架内容当时没有完全解析,因此 DOM 内容尚不可用。

您应该尝试连接到 void QWebFrame::loadFinished ( bool ok ) 并在那里进行 DOM 爬行。

关于c++ - Qt html解析没有找到任何标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9666890/

相关文章:

c++ - 如何将 QAbstractItemModel 写成格式化文本?

c++ - 如何在数组中存储地址 - C++?

c++ - 如何在 Visual Studio 的 CMake 中获取当前配置(发布/调试)

c++ - open()返回结果码的区别: 25 vs 3?

javascript - 悬停展开导航,而不是点击触发器?

html - Samsung Edge 6 中的媒体查询问题

C++ XInput 和 XBOX 360 Controller 从不连接

c++ - 为什么使用 QQuickWindow::grabWindow() 会导致窗口变成图像?

c++ - 在 C++ 中绕过 operator new 的重写

javascript - 使用 jQuery 或 PHP 将 CSS 应用于 html 文档中的单词/字母