在 Qt 中,我将如何解析充满 BAD html 的页面上的所有“a”html 标签“href”属性?
我会使用内置的 QtWebKit。不知道它的性能如何,但我认为它应该捕获所有“坏”HTML。
就像是:
class MyPageLoader : public QObject
{
Q_OBJECT
public:
MyPageLoader();
void loadPage(const QUrl&);
public slots:
void replyFinished(bool);
private:
QWebView* m_view;
};
MyPageLoader::MyPageLoader()
{
m_view = new QWebView();
connect(m_view, SIGNAL(loadFinished(bool)),
this, SLOT(replyFinished(bool)));
}
void MyPageLoader::loadPage(const QUrl& url)
{
m_view->load(url);
}
void MyPageLoader::replyFinished(bool ok)
{
QWebElementCollection elements = m_view->page()->mainFrame()->findAllElements("a");
foreach (QWebElement e, elements) {
// Process element e
}
}
使用该类
MyPageLoader loader;
loader.loadPage("http://www.example.com")
然后对集合做任何你喜欢的事情。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)