2016-12-15 2 views
0

저는 yahoo finance에서 매우 간단한 csv 파일을 구문 분석하려고합니다. 결과 csv는 주식의 현재 가격을 가지고 있습니다. 이 스크립트를 실행하면이 오류가 계속 발생합니다. 내 구문에 뭔가 이상한 점이 있습니까?NodeJS를 사용하여 Yahoo Finance CSV를 구문 분석하십시오.

/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)})}) 
+2

을 처리 리디렉션 필요? –

+0

NodeJS 4.3을 사용하고 있습니다 – rustyocean

+1

이 버전이 화살표 기능을 지원하지 않는다고 생각합니다 .... 업그레이드 또는'function() {}'으로 변경하십시오 –

답변

1

나는이 다운로드를 할 것이라 생각합니다. 또한이 솔루션을 기반으로하며 사용중인 노드의 어떤 버전

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()); 
    } 
}) 
+0

함수 {}를 사용해야했습니다. :) – rustyocean

관련 문제