Created
April 13, 2018 18:14
-
-
Save wildskyf/cdf76994a995706510a485a77801feb6 to your computer and use it in GitHub Desktop.
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
// Kaohsiung 100 - 103 Popularity Parser | |
// fetch data from http://cabu.kcg.gov.tw/Stat/StatRpts/StatRpt1.aspx?yq=103&mq=1&dq=64000030 | |
// | |
// LICENSE: MIT by Geng Zhi Fann | |
// | |
var fetch = require('node-fetch') | |
var cheerio = require('cheerio') | |
var fs = require('fs') | |
const iconv = require('iconv-lite'); | |
var global_names; | |
var year = 103; | |
var req = mq => { | |
return new Promise ( resolve => { | |
fetch(`http://cabu.kcg.gov.tw/Stat/StatRpts/StatRpt1.aspx?yq=${year}&mq=${mq}&dq=64000030`) | |
.then( res => res.text() ) | |
.then( body => { | |
const $ = cheerio.load(body) | |
var arr = []; | |
var names = []; | |
for(var i = 0 ; i < 41*6 ; i ++) { | |
if (i % 6 == 0) { | |
names.push($($('.statis-table tr td')[i]).text().trim()) | |
} | |
if ((i+3) % 6 == 0) { | |
arr.push($($('.statis-table tr td')[i]).text().trim()) | |
} | |
} | |
names = names.filter(a=>a) | |
arr = arr.filter(a=>a) | |
if (names.length !== 41) { | |
names.splice(26,0,'復興里'); | |
} | |
if (arr.length !== 41) { | |
arr.splice(26,0,'0') | |
} | |
arr = arr.map(a => parseInt(a.replace(/\,/g,''))) | |
global_names = names; | |
resolve(arr) | |
}) | |
}) | |
} | |
Promise.all([1,2,3,4,5,6,7,8,9,10,11,12].map(i => req(12))).then( a => { | |
var each_li = a.reduce( (m1, m2) => { | |
if (m1) { | |
return m1.map( (mm1, i) => { | |
return mm1 + m2[i]; | |
}); | |
} | |
}) | |
var output = global_names.map( (v,i) => { | |
return v+','+ each_li[i] | |
}).join('\n'); | |
fs.writeFile('/Users/wildsky/Desktop/'+year+'.csv', iconv.encode( output , 'big5') , (err, a) => { | |
console.log('done', err, a) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment