How to use HTML Agility pack

后端 未结 7 1784
闹比i
闹比i 2020-11-21 04:30

How do I use the HTML Agility Pack?

My XHTML document is not completely valid. That\'s why I wanted to use it. How do I use it in my project? My project is in C#.

7条回答
  •  臣服心动
    2020-11-21 05:16

    HtmlAgilityPack uses XPath syntax, and though many argues that it is poorly documented, I had no trouble using it with help from this XPath documentation: https://www.w3schools.com/xml/xpath_syntax.asp

    To parse

    Jack

    Roy

    I did this:

    string url = "http://website.com";
    var Webget = new HtmlWeb();
    var doc = Webget.Load(url);
    foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//h2//a"))
    {
      names.Add(node.ChildNodes[0].InnerHtml);
    }
    foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//li[@class='tel']//a"))
    {
      phones.Add(node.ChildNodes[0].InnerHtml);
    }
    

提交回复
热议问题