2

I'm trying to learn how to use CsQuery to traverse a dom to get specific text.

The html looks like this:

<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">

My code attempt looks like this:

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();
                }
            }
        }

The first select for .featured-row works but then i don't know how to get down to the .artist to get the text.

Andy
  • 503
  • 3
  • 15
  • 29
  • If it's a true jQuery port, than you should be able to use .find("selector") again. – C Bauer Sep 04 '14 at 12:56
  • One thing to note: "**Except as expressly authorized by AMN, you agree not to** reproduce, link to (via hyperlinks or otherwise), **scrape**, modify, rent, copy, lease, loan, sell, distribute, mirror, frame, syndicate, cache, store, record, publish, download, transmit, display or create derivative works of the AMN Property, **in whole or in part, by any means**." - http://www.allmusic.com/terms-of-service #NotALawyer – dav_i Sep 04 '14 at 14:03
  • I'm not gonna use this, it's just for my personal training. – Andy Sep 04 '14 at 14:05

2 Answers2

1

You should try something similar to this:

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 is port of JQuery so you can google for JQuery code

UPDATE: To traverse to get all artists and titles

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
}
Radenko Zec
  • 7,659
  • 6
  • 35
  • 39
  • Thank you Radenko. But what if i want to loop through the dom to get the artis and title for each record? – Andy Sep 04 '14 at 13:49
0
List<string> artists = html[".featured .artist a"].Select(dom=>dom.TextContent).ToList();

where html == your CQ object.

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

should be

var odd = row.Cq().Find(".featured.odd");
nazarkin659
  • 503
  • 3
  • 11