我如何使用HTMLAgilityPack来提取我想要的值

c# html-agility-pack html-parsing

对于给定的HTML,我想要id的值

 <div class="name" id="john-5745844">
 <div class="name" id="james-6940673">

更新这是我目前所拥有的

    HtmlDocument htmlDoc = new HtmlDocument();
    htmlDoc.Load(new StringReader(pageResponse));
    HtmlNode root = htmlDoc.DocumentNode;

    List<string> anchorTags = new List<string>();
    foreach (HtmlNode div in root.SelectNodes("//div[@class='name' and @id]"))
    {
        HtmlAttribute att = div.Attributes["id"];
        Console.WriteLine(att.Value);
    }

我得到的错误是在foreach行说明: Object reference not set to an instance of an object.我相信这部分是错的"//div[@class='name' and @id]"

一般承认的答案

从示例页面修改:

HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm"); //or whatever HTML file you have
foreach(HtmlNode div in doc.DocumentNode.SelectNodes("//div[@class='name' and @id]")
{
   HtmlAttribute att = div["id"];
   //Do something with att.Value
}


Related

许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow