Chrome, JavaScript, webdev

Yahoo Finance API With NodeJS

nodejs logoIn the past I’ve wrote this post on the different options you can use with Yahoo Finance API. It is time (4 years later!) to a followup post on how to gain more data but this time with NodeJS.
The first idea was to be able to gain information by using different parameters and downloading csv files from Yahoo finance. Something like this GET request will do the magic: and from there you can work with the data.

However, there are cases, where you wish to have information that is not part of this set of arguments. Here are two quick examples for such cases:

In the code below you could see that with the power of NodeJS community and the powerful google sheet API, you can fetch this data and use it in your google sheets. You will need to install NodeJS (of course) and on top of it 3 packages:

  1. npm install requestSo we could work with URLs in an easy way.
  2. npm install cheerio – jQuery for the server side. This will make the code 10x shorter.
  3. npm install edit-google-spreadsheet – to integrate with Google docs/sheets with 2-3 lines of code.

After you finish the installation process, you should download the gist and open it in your favorite editor. Now, if you have a good sheet you wish to use, just add its name to the places I’ve wrote TODO. The results will be a sheet that is full with interesting data in two columns. You can take this example and fork it so it will bring you the information you wish to see.

The code

* Fetch data from the web and save it into a Google Sheet document
* Requirements:
1. NodeJS
2. npm install request
3. npm install cheerio
4. npm install edit-google-spreadsheet
* Author: Ido Green |
* Date: 15 Aug 2013
var request = require('request');
var cheerio = require('cheerio');
var Spreadsheet = require('edit-google-spreadsheet');
// Some parameters
// TODO: move from the global scope
var ticker = "LVS";
var yUrl = "" + ticker;
var financeDetails = new Array();
var keyStr = new Array();
// Upload our data to G-Sheet
function sheetReady(err, spreadsheet) {
if (err) throw err;
spreadsheet.add({ 1: { 1: "Attribute" } });
spreadsheet.add({ 1: { 2: "Value" } });
2: {
1: keyStr
2: {
2: financeDetails
spreadsheet.send(function(err) {
if(err) throw err;
console.log("Updated " + financeDetails.length + " Items with data");
// The main call to fetch the data, parse it and work on it.
request(yUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
// the keys - We get them from a certain class attribute
var td = $('.yfnc_tablehead1');
$(td).each(function(j, val) {
keyStr[j] = $(val).text();
// the values
// TODO: normalize them
var tData = $('.yfnc_tabledata1');
$(tData).each(function(j, val) {
financeDetails[j] = $(val).text();
// Let's do something with the data we have
for (var i=0; i < financeDetails.length; i++) {
console.log (i + ") " + keyStr[i] + " " + financeDetails[i]);
// upload our data to Google sheet
// yFinanceDataPerComapy
debug: true,
username: 'TODO-fill',
password: 'TODO-fill',
debug: true,
spreadsheetName: 'TODO-yourSheetName',
worksheetName: 'TODO-Sheet1orAbetterName',
callback: sheetReady
}); // -- end of request --

view of mountains with trees in the snow

Happy Hacking.


3 thoughts on “Yahoo Finance API With NodeJS

  1. Awesome!!!!
    Really liked the speed. One query though, when i ran the code google api mentioned it didnt find the spreadsheet and then the sheet, wont the api create a new spreadsheet and new worksheet and fill the data accordingly.

  2. Pingback: APIs, NodeJS and Google Docs (GDL-Israel) - TI Nacional

  3. Pingback: Yahoo Finance (hidden) API – 极智投研

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s