在Qt中解析HTML的最佳方式? [英] Best way to parse HTML in Qt?

查看:904
本文介绍了在Qt中解析HTML的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在Qt中解析所有的ahtml标签href属性在一个页面上的BAD html?

How would I go about parsing all of the "a" html tags "href" properties on a page full of BAD html, in Qt?

推荐答案

我会使用内置的QtWebKit。不知道它在性能方面如何,但我认为它应该捕获所有坏的HTML。
类似的东西:

I would use the builtin QtWebKit. Don't know how it does in terms of performance, but I think it should catch all "bad" HTML. Something like:

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")

和然后对集合做任何你喜欢的。

and then do whatever you like with the collection.

这篇关于在Qt中解析HTML的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆