Fork me on GitHub
C# 语言的两个html解析器

基于C# 语言的两个html解析器

 

基于C# 语言的两个html解析器

 

1)Html Agility Pack

http://nsoup.codeplex.com/

代码段示例:

 

复制代码
HtmlDocument doc = new HtmlDocument();
 doc.Load("file.htm");
 foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
 {
    HtmlAttribute att = link["href"];
    att.Value = FixLink(att);
 }
 doc.Save("file.htm");
复制代码

 

 

2) JSoup的Net移植版本 NSoup

http://htmlagilitypack.codeplex.com/

更推荐NSoup

复制代码
NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(HtmlString);
 
NSoup.Nodes.Document doc = NSoup.NSoupClient.Connect("http://www.oschina.net/").Get();
 
ebClient webClient = new WebClient();
String HtmlString=Encoding.GetEncoding("utf-8").GetString(webClient.DownloadData("http://www.oschina.net/"));
NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(HtmlString);
 
WebRequest webRequest=WebRequest.Create("http://www.oschina.net/");
NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(webRequest.GetResponse().GetResponseStream(),"utf-8");
复制代码

 

 

 
 
posted on 2014-10-12 20:23  HackerVirus  阅读(498)  评论(0编辑  收藏  举报