简体   繁体   中英

HTML Agility Pack Parsing div

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 ?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM