Search code examples
node.jscsvweb-scraping

Parse/save/get csv response from URL with NodeJS


I'm trying to get the results from a Yahoo finance URL:

http://finance.yahoo.com/d/quotes.csv?s=XOM=sn1yr which returns:

"XOM",4:00pm - <b>83.25`</b>`,2.11,13.42

I'm trying to store these results in a database using Mongoose. I've got some code working that saves from an existing CSV file(with different but related contents as you can see):

 var writer = new csv.CsvWriter(process.stdout);
  reader.setColumnNames(["Symbol","Company","StockExchange"]);
  reader.addListener('data', function(data) {
  console.log(data);
  var stock = new Stock(data).save();
 });

I'm trying to do something like:

Scraper is a module found at https://github.com/mape/node-scraper

scraper('http://search.twitter.com/search?q=javascript', function(err, $) {
if (err) {throw err;}

$('.msg').each(function() {
    console.log($(this).text().trim()+'\n');
});
 });

but with the CSV results from the Yahoo URL without putting the response into a separate CSV file and was told I need to use stream.write somehow. I'm new to Node and would appreciate some help figuring this out. Thanks in advance for the help!


Solution

  • So you're trying to grab CSV from http://finance.yahoo.com/d/quotes.csv?s=XOM=sn1yr and parse the data so that you can store it in a database?

    My scraping library node.io might be able to help.

    require('node.io').scrape(function() {
        this.get('http://finance.yahoo.com/d/quotes.csv?s=XOM=sn1yr', function(err, data) {
            var lines = data.split('\n');
            for (var line, i = 0, l = lines.length; i < l; i++) {
                line = this.parseValues(lines[i]);
    
                console.log(line); //['XOM, '4:00pm - <b>83.25`</b>`', '2.11', '13.42']
    
                //Do something with your data..
            }
        });
    });