2.Code
using HtmlAgilityPack;
using System;
string url = "https://www.baidu.com";
HtmlWeb web = new HtmlWeb();
//从url中加载
HtmlDocument doc = web.Load(url);
//获得title标签节点,其子标签下的所有节点也在其中
HtmlNode headNode = doc.DocumentNode.SelectSingleNode("//title");
//获得title标签中的内容
string Title = headNode.InnerText;
Console.WriteLine(Title);
Console.WriteLine(".........................................");
//获得id选择器为u1标签(是u1非ul(L))节点
HtmlNode aNode = doc.DocumentNode.SelectSingleNode("//div[@id='s-top-left']");
//获得ul标签下的所有子节点
HtmlNodeCollection aCollection = aNode.ChildNodes;
foreach (var item in aCollection)
{
//获得标签属性为href的值``
string aValue = item.Attributes["href"].Value;
//获得标签内的内容
string aInterText = item.InnerText;
Console.WriteLine("属性值:" + aValue + "\t" + "标签内容:" + aInterText);
}
网友评论