官網地址:htmlagilitypack
百度網盤下載地址:點擊
使用方法:
1.引用HtmlAgilityPack.dll文件
2.引用命名空間:
using HtmlAgilityPack;
3.調用(元素查找方式為xpath,用法參見w3school):
static void Main(string[] args) { string html = GetHtml("http://www.w3school.com.cn/xpath/xpath_syntax.asp"); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); HtmlNode node = doc.DocumentNode; HtmlNode div = node.SelectNodes("//table[@class='dataintable']")[0]; Console.WriteLine(div.InnerHtml); Console.Read(); } static string GetHtml(string url) { WebRequest request = WebRequest.Create(url); WebResponse res = request.GetResponse(); StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8); string html = sr.ReadToEnd(); sr.Close(); res.Close(); return html; }