首页 > 解决方案 > C# 从 html 中获取值

问题描述

我有这个 html

     <a class="video" style="display: block" href="some text">
                            <img class="pic" src="" alt="">
                            <div class="title">some text</div>
                    </a>
    
    <a class="video" style="display: block" href="some text">
                            <img class="pic" src="" alt="">
                            <div class="title">some text</div>
                    </a>

它多次下降。如何从href标题中获取值?

我试过这个,但我不知道如何得到<div class="title">

foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//a[@class='video']")) 
{ 
Console.WriteLine("node:" + node.GetAttributeValue("href", null));
}

标签: c#html-agility-pack

解决方案


看看这是否适合你。

foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//a[@class='video']")) 
{ 
    Console.WriteLine("href: " + node.GetAttributeValue("href", null));
    Console.WriteLine("title: " + node.SelectSingleNode("./div[@class='title']").InnerText);
}

推荐阅读