0

I'm new to using jsdom and querystring. I'm trying to scrape a page for all of the soundcloud track_id's within all of the iframe html tags. The code below logs undefined because the first iframe is not a soundcloud player.

How do I...

  1. Modify the code to retrieve all of the iframe instances
  2. Check if http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F or just soundcloud is present using jsdom/qs. (I can use an if string contains string type of js test, but I figure there is a better way using jsdom/qs).

    jsdom.env({
     html: 'A_URL',
     scripts: [
       'http://code.jquery.com/jquery-1.5.min.js'
     ],
     done: function(errors, window) {
      var $ = window.$;
      var src = $('iframe').attr('src');
      var aRes = qs.parse(decodeURIComponent(url.parse(src).query)).url.split('/');
      var track_id = aRes[aRes.length-1];
    
     console.log("track_id =", track_id);
     }
    });
    
mnort9
  • 1,810
  • 3
  • 30
  • 54

1 Answers1

0
$('iframe').each(function(index, element) {
  if (element.attr['src'].match(/soundcloud/)) {
    // do your stuff
    return false; // if there's only one relevant iframe
  }
}
ebohlman
  • 14,795
  • 5
  • 33
  • 35
  • I think this needs an extra ')' after soundcloud argument. – mnort9 Jul 19 '12 at 19:56
  • Thanks, for your help. How do I check if 'src' exists in the iframe? – mnort9 Jul 19 '12 at 19:57
  • Missing paren fixed in edit. Normally an `iframe` would always have a `src`, but if there's some case where it might not, just change the test to `if (element.attr[src] && ...`. – ebohlman Jul 20 '12 at 00:37
  • I keep getting `src is not defined` when I know there is 10+ iframes with `src` on the page. – mnort9 Jul 20 '12 at 04:03
  • Is that after executing the original line: `var src = $('iframe').attr('src');` or did you change your code? (note that if it's the original and you have more than one iframe, `src` will be set to the "src" attribute of the first one (or `undefined` if it doesn't have one). That's why you need to use `$('iframe').each()`; `$('iframe')` can return a jQuery object matching multiple elements, and if you apply a method such as `attr` to such an object it only deals with the first element in the collection). – ebohlman Jul 20 '12 at 05:37
  • No I removed that line. The only code I have in the `.each` function is `if (element.attr['src'])`. – mnort9 Jul 20 '12 at 06:04