【问题标题】:html agility, parse div htmlhtml 敏捷,解析 div html
【发布时间】:2014-02-04 08:16:30
【问题描述】:

我有一个 html 页面,我正在使用 html 敏捷性进行处理,这是页面的结构。 div 类是“show1”、“show2”等等。是的,名称保持不变。如何加载名称为 song_html 的所有 div 或带有“show”+number 的类,然后是每个内部 div 的值(歌曲信息、标题和超链接值。)

<div id="container">

<div class="show1" id="song_html">
  <div class="left">
  Info1             
  </div>
<div id="right_song">
  <div style="font-size:15px;">
       <b>Song Title</b>
  </div>
  <div style="float:left;">
            <a style="color:green;" target="_blank" rel="nofollow" 
            href="linktofile">Download</a>     
   </div> 
</div>                      
</div>                      


<div class="show2" id="song_html">
  <div class="left">
  Info2             
  </div>
<div id="right_song">
  <div style="font-size:15px;">
       <b>Song Title 2</b>
  </div>
  <div style="float:left;">
            <a style="color:green;" target="_blank" rel="nofollow" 
            href="linktofile">Download</a>     
   </div> 
</div>                      
</div>                      


</div>

我们将不胜感激。这就是我到目前为止所做的。

  var nodes = doc.DocumentNode.Descendants("div").Where(d => d.Attributes.Contains("class") &&   d.Attributes["class"].Value.Contains("show"));

foreach (HtmlNode node in nodes)
                {


}

问候 督导员

【问题讨论】:

  • 您有多个带有id="right_song"id="song_html" 的div,对吗?

标签: html c#-4.0 html-agility-pack


【解决方案1】:

试试这个:

var songs = htmlDoc.DocumentNode.Descendants("div")
    .Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("show"))
    .Select(d => new 
    {
        Song = d.Descendants("div").First().InnerText.Trim(),
        Title = d.Descendants("div").ElementAt(1).Descendants("div").First().InnerText.Trim(),
        Link = d.Descendants("div").ElementAt(1).Descendants("a").First().Attributes["href"].Value.Trim()
    });

foreach (var songInfo in songs)
{
    Console.WriteLine("Song: {0} Title: {1} Link: {2}", songInfo.Song, songInfo.Title, songInfo.Link);
}

输出:

Song: Info1 Title: Song Title   Link: linktofile
Song: Info2 Title: Song Title 2 Link: linktofile

【讨论】:

  • 像魅力一样工作。多谢。我希望我能给你 10 票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-13
  • 2010-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-01
  • 2013-07-20
相关资源
最近更新 更多