使用CsQuery遍历dom

时间:2014-09-04 12:46:12

标签: c# csquery

我正在尝试学习如何使用CsQuery遍历dom以获取特定文本。

html看起来像这样:

<div class="featured-rows">
  <div class="row">
    <div class="featured odd" data-genres-filter="MA0000002613">
      <div class="album-cover">
      <div class="artist">
        <a href="http://www.allmusic.com/artist/half-japanese-0000555654">Half apanese</a>
      </div>
      <div class="title">
      <div class="label"> Joyful Noise </div>
      <div class="styles">
      <div class="rating allmusic">
      <div class="rating average">
      <div class="headline-review">
    </div>
    <div class="featured even" data-genres-filter="MA0000002572, MA0000002613">
    </div>
  <div class="row">
  <div class="row">
  <div class="row">

我的代码尝试如下:

public void GetRows()
        {
            var artistName = string.Empty;
            var html = GetHtml("http://www.allmusic.com/newreleases");            
            var rows = html.Select(".featured-rows");
            foreach(var row in rows)
            {     
                var odd = row.Cq().Find(".featured odd");
                foreach(var artist in odd)
                {
                    artistName = artist.Cq().Text();
                }
            }
        }

.featured-row的第一个选择有效,但后来我不知道如何找到.artist来获取文本。

2 个答案:

答案 0 :(得分:1)

你应该尝试类似的东西:

var html = GetHtml("http://www.allmusic.com/newreleases");
var query = CQ.Create(html)
var row = query[".artist>a"];
string link = row.Attributes["href"];
string text = row.DefaultValue or row.InnerText or row.Value...

CsQuery是JQuery的端口,因此您可以谷歌搜索JQuery代码

更新: 遍历以获得所有艺术家和头衔

var rows = query[".featured odd"];
foreach(var row in rows)
{
  var artistsLink = row[".artists>a"];
  var title = row[".title"];
 // here do whatever you need with this
}

答案 1 :(得分:0)

List<string> artists = html[".featured .artist a"].Select(dom=>dom.TextContent).ToList();

其中html ==您的CQ对象。

var odd = row.Cq().Find(".featured odd");

应该是

var odd = row.Cq().Find(".featured.odd");