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.
You can use
SelectNodeswhich search inside the HTML tree and return a list of nodes matching by the XPath expressionInstead 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