码迷,mamicode.com
首页 > Web开发 > 详细

HtmlAgilityPack开发

时间:2017-08-06 23:03:25      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:from   tor   val   nbsp   parse   blog   安装   span   nuget   

官方网站:

http://html-agility-pack.net/

Nuget安装:

Install-Package HtmlAgilityPack

C# HTML Parser Examples:

// From File
var doc = new HtmlDocument();
doc.Load(filePath);

// From String
var doc = new HtmlDocument();
doc.LoadHtml(html);

// From Web
var url = "http://html-agility-pack.net/";
var web = new HtmlWeb();
var doc = web.Load(url);

C# HTML Selectors Examples:

// With XPath    
var value = doc.DocumentNode
    .SelectNodes("//td/input")
    .First()
    .Attributes["value"].Value;
    
// With LINQ    
var nodes = doc.DocumentNode.Descendants("input")
    .Select(y => y.Descendants()
    .Where(x => x.Attributes["class"].Value == "box"))
    .ToList();

示例代码:

HtmlWeb webClient = new HtmlWeb();
HtmlDocument doc = webClient.Load("http://www.cnsos.net/weburl/");

HtmlNodeCollection hrefList = doc.DocumentNode.SelectNodes(".//a[@href]");

if (hrefList != null)
{
    foreach (HtmlNode href in hrefList)
    {
        HtmlAttribute att = href.Attributes["href"];
        Console.WriteLine(att.Value);
    }
}

 

HtmlAgilityPack开发

标签:from   tor   val   nbsp   parse   blog   安装   span   nuget   

原文地址:http://www.cnblogs.com/wzwyc/p/7296106.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!