且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

htmlagilitypack解析链接和内部文本

更新时间:2023-12-05 18:07:46

如果您熟悉 XPATH ,您将能够浏览html的元素和属性以获取所需的内容.要获取上面的每个href,您可以编写如下代码:

If you are familiar with XPATH you will be able to navigate through the elements and attributes of the html to get whatever you want. To get each href in the above you could write code as follows:

 const string xpath = "/div//span/a";

 //WebPage below is a string that contains the text of your example
 HtmlNode html = HtmlNode.CreateNode(WebPage);
 //The following gives you a node collection of your two <a> elements
 HtmlNodeCollection items = html.SelectNodes(xpath);
 foreach (HtmlNode a in items)
 {    
      if (a.Attributes.Contains("href"))
      //Get your value here
      {
           yourValue = a.Attributes["href"].Value
      }
 }

注意:我尚未运行或测试此代码

Note: I have not run or tested this code