Created
June 27, 2014 16:33
-
-
Save luccastera/7e3308b7f157ca4d8fcc to your computer and use it in GitHub Desktop.
World Cup Rosters
This file contains hidden or 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 request = require('request'), | |
cheerio = require('cheerio'); | |
if (process.argv.length !== 3) { | |
console.log('Usage: node roster.js <country>'); | |
process.exit(1); | |
} | |
var country = process.argv[2]; | |
console.log('Fetching roster for ' + country + '...'); | |
var url = 'http://en.wikipedia.org/wiki/' + country + '_national_football_team'; | |
request.get(url, function(err, response, body) { | |
if (err) { | |
console.log(err); | |
process.exit(1); | |
} | |
var htmlBeginning = '<span class="mw-headline" id="Current_squad">Current squad</span>'; | |
var htmlEnd = '<span class="mw-headline" id="Recent_call-ups">Recent call-ups</span>'; | |
var tempBody = body.split(htmlBeginning)[1]; | |
tempBody = tempBody.split(htmlEnd)[0]; | |
tempBody = tempBody.slice(5, -5); | |
var $ = cheerio.load(tempBody); | |
var table = $('table.sortable'); | |
var $table = cheerio.load(table.html()); | |
var players = []; | |
$table('tr').map(function(index, row) { | |
players[index] = {}; | |
$(this).children().each(function(i, player) { | |
if (i === 2) { | |
players[index].name = $(this).text(); | |
} | |
if (i === 4) { | |
players[index].age = $(this).text(); | |
} | |
if (i === 6) { | |
players[index].club = $(this).text(); | |
} | |
}); | |
}); | |
console.log('\n'); | |
players.forEach(function(player) { | |
if (player && player.name && player.name !== 'Player') { | |
console.log(player.name, ',', player.age, ',', player.club) | |
} | |
}); | |
console.log('\n'); | |
process.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment