Created
April 23, 2014 10:15
-
-
Save gasolin/11209737 to your computer and use it in GitHub Desktop.
http://axe.g0v.tw/level/1 crawer result test in http://jsonlint.com/
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 http = require("http"); | |
var cheerio = require("cheerio"); | |
/** | |
* Utility function that downloads a URL and invokes | |
* callback with the data. | |
*/ | |
function download(url, callback) { | |
http.get(url, function(res) { | |
var data = ""; | |
res.on('data', function (chunk) { | |
data += chunk; | |
}); | |
res.on("end", function() { | |
callback(data); | |
}); | |
}).on("error", function() { | |
callback(null); | |
}); | |
} | |
// DO THE JOB | |
var url = "http://axe-level-1.herokuapp.com/"; | |
var resultJson = []; | |
var column_title = []; | |
download(url, function(data) { | |
if (data) { | |
// console.log(data); | |
var $ = cheerio.load(data); | |
$("tr").each(function(i, e) { | |
if(i === 0) { | |
var columns0 = $(e).find("td"); | |
columns0.each(function(j, e) { | |
var order = j+1; | |
if(order > 1) { | |
column_title.push($(e).text()); | |
} | |
}); | |
console.log(column_title); | |
} else { | |
var columns = $(e).find("td"); | |
// column holder | |
var person = {name: "", grades: {}}; | |
columns.each(function(j, e) { | |
if (j === 0) { | |
person.name = $(e).text(); | |
} else { | |
person.grades[column_title[j-1]] = parseInt($(e).text()); | |
} | |
}); | |
resultJson.push(person); | |
} | |
}); | |
console.log(JSON.stringify(resultJson)); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment