Skip to content

Instantly share code, notes, and snippets.

@you21979
Created August 2, 2013 11:42
Show Gist options
  • Save you21979/6139271 to your computer and use it in GitHub Desktop.
Save you21979/6139271 to your computer and use it in GitHub Desktop.
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);
});
@you21979
Copy link
Author

node.jsはクローラー作るのに最適な言語だと思う。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment