Skip to content

Instantly share code, notes, and snippets.

@SantoshSrinivas79
Created June 23, 2012 05:05
Show Gist options
  • Save SantoshSrinivas79/2976964 to your computer and use it in GitHub Desktop.
Save SantoshSrinivas79/2976964 to your computer and use it in GitHub Desktop.
Scraping Historical Data of NSE Indices using CasperJS
var casper = require('casper').create({
verbose: true,
logLevel: "debug",
clientScripts: ['includes/jquery-1.7.2.min.js','includes/moment.min.js']
});
var indices = [
"S&P CNX NIFTY",
"S&P CNX 500",
"CNX 100",
"CNX 200",
"CNX NIFTY JUNIOR",
"NIFTY MIDCAP 50",
"CNX MIDCAP",
"CNX SMALLCAP",
"BANK NIFTY",
"CNX DIVIDEND OPPORTUNITIES",
"CNX ENERGY",
"CNX FINANCE",
"CNX FMCG",
"CNX INFRA",
"CNX IT",
"CNX MNC",
"CNX PHARMA",
"CNX PSE",
"CNX PSU BANK",
"CNX REALTY",
"CNX SERVICE",
"S&P CNX DEFTY",
"S&P CNX NIFTY DIVIDEND",
"S&P ESG INDIA INDEX",
"S&P CNX NIFTY SHARIAH",
"S&P CNX 500 SHARIAH",
"CNX AUTO",
"CNX COMMODITIES",
"CNX CONSUMPTION",
"CNX METAL",
"CNX MEDIA"
];
// This function will do the page browse for the index value
browseIndex = function(indexVal){
console.log('Browsing for:'+indexVal);
casper.thenOpen('http://www.nseindia.com/content/indices/ind_histvalues.htm', function() {
casper.echo("Now I'm trying for:" + indexVal);
var currDate = this.evaluate(function() {
return moment().format("DD-MM-YYYY");
});
// console.log('Todays date is: ' + currDate);
casper.fill("form[name='indform']", {
'indexType': indexVal,
'fromDate': '01-01-1980',
'toDate': currDate
}, true);
casper.then(function() {
console.log('Going to download for: ' + indexVal);
downloadIndex(indexVal);
});
});
};
// This function will download the index file
downloadIndex = function(indexVal){
console.log('Downloading file for:'+indexVal);
var url = casper.evaluate(function() {
// return __utils__.findOne('a[href*="csv"]').getAttribute("href");
return $('a[href*="csv"]').attr('href');
});
var fs = require('fs');
casper.echo(url);
console.log(fs.workingDirectory+'/../../../../'+'Public/casperjs/nseindia/'+ indexVal+'.csv');
casper.download(url, fs.workingDirectory+'/../../../../'+'Public/casperjs/nseindia/'+ indexVal+'.csv');
};
casper.start();
casper.each(indices, function(self, index) {
console.log('Trigger Browsing for:'+index);
browseIndex(index);
});
casper.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment