Created
August 2, 2013 11:42
-
-
Save you21979/6139271 to your computer and use it in GitHub Desktop.
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 http = require('http'); | |
function get(callback){ | |
var opt = { | |
host: 'ichart.finance.yahoo.com', | |
port: 80, | |
path: '/table.csv?s=AAPL&a=11&b=1&c=2007&d=12&e=1&f=2012&g=m&ignore=.csv', | |
method: 'GET', | |
agent: new http.Agent({maxSockets: 1}) | |
}; | |
var req = http.request(opt, function(res) { | |
var data = ''; | |
res.on('data', function(chunk) { | |
data += chunk; | |
}); | |
res.on('end', function() { | |
callback(parsecsv(data)); | |
}); | |
}); | |
req.end(); | |
} | |
function parsecsv(data){ | |
var begin = 1; | |
var count = 0; | |
var header = []; | |
var table = []; | |
data.toString().split('\n').forEach(function(line){ | |
if(line !== ''){ | |
var n = 0; | |
var obj = {}; | |
line.split(',').forEach(function(col){ | |
if(count >= begin){ | |
if(n === 0){ | |
obj[header[n]] = col; | |
}else{ | |
obj[header[n]] = parseFloat(col); | |
} | |
table.push(obj); | |
}else{ | |
header.push(col); | |
} | |
++n; | |
}); | |
} | |
++count; | |
}); | |
return table; | |
} | |
get(function(table){ | |
// 最終結果表示 | |
console.log(table); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
node.jsはクローラー作るのに最適な言語だと思う。