簡體   English   中英

我將如何使用HTMLAgilityPack提取所需的值

[英]How would I use HTMLAgilityPack to extract the value I want

對於給定的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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM