网页采集工具-HtmlAgilityPack使用指南

官网地址:http://html-agility-pack.net/

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();

 

posted on 2017-08-15 15:50  冒得味口  阅读(128)  评论(0编辑  收藏  举报