using HtmlAgilityPack; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Xml; namespace ConsoleApp2.test1 { public class Class1 { //http://www.w3school.com.cn/xpath/index.asp //http://www.w3school.com.cn/xpath/xpath_syntax.asp public void test1() { //// 从物理路径的文件加载 //var doc = new HtmlDocument(); //doc.Load("./111/1.html");//文件路径 //// 从Stream当中加载 //var doc = new HtmlDocument(); //doc.LoadHtml(html); //// 从网页的Url链接加载 //var url = "http://www.cnblogs.com/xuliangxing/"; //var web = new HtmlWeb(); //var doc = web.Load(url); JumpKick.HttpLib.Http.Get("https://www.cnblogs.com/xuliangxing/p/8004403.html").OnSuccess(result => { //Console.Write(result); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(result); //HtmlNode titleNode = doc.DocumentNode.SelectSingleNode("//a[@id='cb_post_title_url']"); //Console.WriteLine(titleNode.InnerHtml); //解析“div”的“id”为“cnblogs_post_body”,的子节点“h1”,并且位置是第“2”的个。 HtmlNode titleNode = doc.DocumentNode.SelectSingleNode("//div[@id='cnblogs_post_body']/h1[position()=2]"); Console.WriteLine(titleNode.InnerHtml); }).Go(); } } }