'C# extracting a single variable from html document from a website

This is what it looks like. html

I've tried something like this:

var url = "https://www.tek-zence.no/";
var httpsClient = new HttpClient();
var html = await httpsClient.GetStringAsync(url);

var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);

var element = htmlDocument.DocumentNode.Descendants("div")
    .Where(node => !node.GetAttributeValue("class", "").Contains("feature-nummer")).ToString();
Console.WriteLine(element.Innertext);

Any thoughts?



Solution 1:[1]

With HtmlAgilityPack, you can do this:

var text = @"<div><div class='feature-nummer'>01</div></div>";

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(text);

int number = -1;
var div = doc.DocumentNode.SelectSingleNode("//div[contains(@class, 'feature-nummer')]");
if (div != null && int.TryParse(div.InnerText, out int value))
{
    number = value;
}

The HTML here is just a sample like your HTML. Must work with your HTML too.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Victor