Trying to get a specific element from html class using HtmlAgilityPack

34 views Asked by At

I'm trying to get this element from part of a html page. Below is the snippet of html code from the webpage html:

<td class="datatable_cell__LJp3C datatable_cell--align-end__qgxDQ datatable_cell--up__hIuZF min-w-[77px] text-right align-middle text-sm font-normal leading-5 rtl:text-right text-positive-main"
    dir="ltr">
    7,646.16
</td>

The element I'm trying to get is the 7,646.16 figure. below is what I have attempted to do:

using (WebClient client = new WebClient())
{
    string html = client.DownloadString(url);
    HtmlDocument htmlDoc = new HtmlDocument();
    htmlDoc.LoadHtml(html);

    try
    {
        IEnumerable<HtmlNode> nodes = htmlDoc.DocumentNode.Descendants().Where(n => n.HasClass("datatable_cell__LJp3C datatable_cell--align-end__qgxDQ datatable_cell--up__hIuZF min-w-[77px] text-right align-middle text-sm font-normal leading-5 rtl:text-right text-positive-main"));

        try
        {
            foreach (var node in nodes)
            {
                Console.WriteLine(node);
            }
        }
        catch
        {
            Console.WriteLine("Failed Process");
        }
    }
 }

What I want it to do is extract that element (the 7646.16) and display that on the console.

1

There are 1 answers

1
Ibram Reda On

You can use SelectNodes which search inside the HTML tree and return a list of nodes matching by the XPath expression

IEnumerable<HtmlNode> nodes =
       htmlDoc.DocumentNode
       .SelectNodes("//td[@class=\"datatable_cell__LJp3C datatable_cell--align-end__qgxDQ datatable_cell--up__hIuZF min-w-[77px] text-right align-middle text-sm font-normal leading-5 rtl:text-right text-positive-main\"]");

if(nodes is not null)
{
    foreach (var node in nodes)
    {
        Console.WriteLine(node.InnerText.Trim());
    }
}
else
{
    Console.WriteLine("Error: desired nodes is not found");
}

Instead of over using try and catch clauses which is a bad in performance and readability, try to handle your null references with the simple if conditions as shown above