HTML Agility Pack Parsing div

c# html-agility-pack parsing

Question

I'm trying to parse HTML, I need to get "text" from this part:

<div class="_gdf kno-fb-ctx">
    <span data-ved="0ahUKEwjIr9brjO7UAhUnYZoKHda-ALgQ2koIogEoAjAT"> text</span>
</div>

Here's my C# code:

var message = doc.DocumentNode.SelectSingleNode("//div[@class='_gdf kno-fb-ctx']").InnerText;
Console.WriteLine(message);

What I'm doing wrong ?

1
0
7/3/2017 9:59:25 PM

Popular Answer

I see that you are not selecting the actual 'Span' node to read the InnertTex. You have selected div and trying to read InnertTex, which won't give you desired result "Text". Instead you can do like below:

        HtmlAgilityPack.HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml("<div class='_gdf kno-fb-ctx'><span data-ved = '0ahUKEwjIr9brjO7UAhUnYZoKHda-ALgQ2koIogEoAjAT'> text </span ></div >");
        var text = doc.DocumentNode.SelectSingleNode("//div[@class=\"_gdf kno-fb-ctx\"]//span").InnerText;
0
7/4/2017 8:00:14 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow