Last active
August 29, 2015 14:05
-
-
Save n9ti/f2cf46a444de8fdd7351 to your computer and use it in GitHub Desktop.
klogic to JSON
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 request = require('request'); | |
var cheerio = require('cheerio'); | |
request.post( | |
'http://klogic.kmutnb.ac.th:8080/kris/tess/dataQuerySelector.jsp?query=examTab', | |
{ | |
form: { | |
facCode: '06', | |
currCode: '56060214' | |
} | |
}, | |
function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
extractData(body); | |
} | |
} | |
); | |
function extractData(body){ | |
var table = {data: []}; | |
$ = cheerio.load(body); | |
var tr = $('table[border=1]').find('tr'); | |
for(var i=1;i<tr.length;i++) { | |
if($(tr[i]).find('td').length != 3) { | |
continue; | |
} | |
var subject = $(tr[i]).find('td:nth-child(1)').text(); | |
var subjectSplit = subject.split("\n\t\t"); | |
var subject_name = subjectSplit[1]; | |
var subject_credit = subjectSplit[2]; | |
var mid = $(tr[i]).find('td:nth-child(2)').text(); | |
var final = $(tr[i]).find('td:nth-child(3)').text(); | |
var obj = {}; | |
if($(tr[i]).find('td:nth-child(1)').attr('rowspan') == '2'){ | |
var mid2 = $(tr[i+1]).find('td:nth-child(1)').text(); | |
var final2 = $(tr[i+1]).find('td:nth-child(2)').text(); | |
obj = { | |
subject_name: subject_name, | |
subject_credit: subject_credit, | |
mid: [mid, mid2], | |
final: [final, final2] | |
}; | |
}else { | |
obj = { | |
subject_name: subject_name, | |
subject_credit: subject_credit, | |
mid: mid, | |
final: final | |
}; | |
} | |
table.data.push(obj); | |
} | |
console.log(table); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment