Last active
August 29, 2015 14:08
-
-
Save adnasa/a10b681ef27096e6f975 to your computer and use it in GitHub Desktop.
quick and dirty hack to get the discography list from an artist in wikipedia :D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('underscore')._; | |
var casper = require('casper').create({ | |
clientScripts: [ | |
'bower_components/jquery/dist/jquery.min.js', | |
'bower_components/underscore/underscore.js' | |
], | |
verbose: true, | |
logLevel: 'debug' | |
}); | |
casper.start('http://en.wikipedia.org/wiki/Q-Tip_(musician)', function() { | |
var discographyList = this.evaluate(function() { | |
var k = /(d|D)(iscography)/g; // quick and dirty | |
var contentText = $('#mw-content-text'); | |
var headlines = $('h2', contentText); | |
var matchingHeadline = _.find(headlines, function(headline) { | |
return headline.innerHTML.match(k); | |
}); | |
var table = null; | |
if (matchingHeadline) { | |
matchingHeadline = $(matchingHeadline); | |
var next = matchingHeadline.next(); | |
while (next) { | |
if (next[0].nodeName.toLowerCase() !== 'ul') { | |
next = next.next(); | |
} else { | |
table = next; | |
next = null; | |
} | |
} | |
} | |
var discos = []; | |
if (table) { | |
var childElements = $('li', table); | |
_.each(childElements, function(element) { | |
discos.push($(element).text()); | |
}); | |
} | |
return discos; | |
}); | |
utils.dump(discographyList); | |
}); | |
casper.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment