解析雅虎财经CSV用的NodeJS
我试图解析从雅虎财经一个非常简单的csv文件。由此产生的csv只有股票的当前价格。当我运行这个时,我一直得到这个错误。在我的语法中有什么东西吗?解析雅虎财经CSV用的NodeJS
/tmp/de49fef0-c2fb-11e6-b1be-07e013ca5c3c/Node Application/app.js:6 https.get(endpoint, (response) => {^SyntaxError: Unexpected token > at Module._compile (module.js:437:25) at Object.Module._extensions..js (module.js:467:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.runMain (module.js:492:10) at process.startup.processNextTick.process._tickCallback (node.js:244:9)
var https = require('https')
var endpoint = "http://finance.yahoo.com/d/quotes.csv?s=AAPL&f=a"
var body = ""
https.get(endpoint, (response) => {
response.on('data', (chunk) => { body += chunk })
response.on('end',() => {
var data = JSON.parse(body)
console.log(data)})})
我认为这会做你的下载。正是基于这一解决方案和需要重定向您正在使用哪个节点的版本处理
How to download a file with Node.js (without using third-party libraries)?
var http = require('http');
var fs = require('fs');
var endpoint = "http://finance.yahoo.com/d/quotes.csv?s=AAPL&f=a"
var body = ""
var download = function(url, dest, cb) {
var file = fs.createWriteStream(dest);
var request = http.get(url, function(response) {
if(response.statusCode === 301){
console.log();
var request = http.get(response.headers.location, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(cb); // close() is async, call cb after close completes.
});
});
}
}).on('error', function(err) { // Handle errors
fs.unlink(dest); // Delete the file async. (But we don't check the result)
if (cb) cb(err.message);
});
};
download(endpoint, 'test.csv', function(err){
if(err){
console.log(err);
}
else{
//read csv file here
console.log(fs.readFileSync('test.csv').toString());
}
})
我不得不使用功能{} ...这是它:) – rustyocean
? –
我使用4.3的NodeJS – rustyocean
我觉得这个版本不支持箭头功能....尝试升级或更换,以'函数(){}' –