.NET的HTML解析库 [英] HTML Parsing Libraries for .NET

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

问题描述

我正在寻找图书馆来解析HTML以提取链接,表单,标签等。

I'm looking for libraries to parse HTML to extract links, forms, tags etc.

  • http://www.majestic12.co.uk/projects/html_parser.php
  • http://www.netomatix.com/Products/DocumentManagement/HtmlParserNet.aspx
  • http://www.developer.com/net/csharp/article.php/2230091

LGPL或任何其他商业广告开发友好的许可证是可取的。

LGPL or any other commercial development friendly licenses are preferable.

你有没有与这些图书馆有任何经验?或者你可以推荐另一个类似的库吗?

Have you got any experience with one of this libraries? Or could you recommend another similar library?

推荐答案

HTML Agility Pack 具有这种类型的事情的例子,并使用xpath进行熟悉的查询,例如(从主页),查找所有链接都是简单的:

The HTML Agility Pack has examples of exactly this type of thing, and uses xpath for familiar queries - for example (from home page), to find all links is simply:

foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a@href")) {
    //...
}






编辑

截至6/19/2012,上述代码以及唯一的代码示例显示在 HTML敏捷包示例 页面将无法正常工作。只需要稍微调整,如下所示。

HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm");

foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
{
  HtmlAttribute att = link.Attributes["href"];
  att.Value = Foo(att); // fix the link
}
doc.Save("file.htm");

这篇关于.NET的HTML解析库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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