Parsing HTML page with HtmlAgilityPack

后端 未结 2 1055
故里飘歌
故里飘歌 2020-11-27 14:57

Using C# I would like to know how to get the Textbox value (i.e: john) from this sample html script :


2条回答
  •  醉酒成梦
    2020-11-27 15:18

    There are a number of ways to select elements using the agility pack.

    Let's assume we have defined our HtmlDocument as follows:

    string html = @"
    
    Name :
    "; HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(html);

    1. Simple LINQ
    We could use the Descendants() method, passing the name of an element we are in search of:

    var inputs = htmlDoc.DocumentNode.Descendants("input");
    
    foreach (var input in inputs)
    {
        Console.WriteLine(input.Attributes["value"].Value);
        // John
    }
    

    2. More advanced LINQ
    We could narrow that down by using fancier LINQ:

    var inputs = from input in htmlDoc.DocumentNode.Descendants("input")
                 where input.Attributes["class"].Value == "box"
                 select input;
    
    foreach (var input in inputs)
    {
        Console.WriteLine(input.Attributes["value"].Value);
        // John
    }
    

    3. XPath
    Or we could use XPath.

    string name = htmlDoc.DocumentNode
        .SelectSingleNode("//td/input")
        .Attributes["value"].Value;
    
    Console.WriteLine(name);
    //John
    

提交回复
热议问题