Last active
October 7, 2015 04:20
-
-
Save madaboutcode/28d57c3011bf13b3a691 to your computer and use it in GitHub Desktop.
casperjs - bsedownload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var casper = require("casper").create({ | |
pageSettings: { | |
webSecurityEnabled: false | |
} | |
}); | |
var x = require('casper').selectXPath; | |
var fs = require('fs'); | |
function printArgs() { | |
var i, ilen; | |
for (i = 0, ilen = arguments.length; i < ilen; ++i) { | |
console.log(" arguments[" + i + "] = " + JSON.stringify(arguments[i])); | |
} | |
console.log(""); | |
} | |
phantom.clearCookies(); | |
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36'); | |
casper.on("page.error", function(pageErr){ | |
this.echo("page.err: " + JSON.stringify(pageErr)); | |
}); | |
casper.on('resource.requested', function(requestData, request) { | |
if ((/\.(css).*/gi).test(requestData['url']) || | |
requestData.headers['Content-Type'] == 'text/css') { | |
// casper.echo('[cancelled] '+requestData['url']); | |
request.abort(); | |
return; | |
} | |
if(requestData['url'].indexOf('www.bseindia.com') < 0) { | |
request.abort(); | |
return; | |
} | |
if ((/\.(png|jpg|gif).*/gi).test(requestData['url'])) { | |
// casper.echo('[cancelled] '+requestData['url']); | |
request.abort(); | |
return; | |
} | |
casper.echo('[requested] '+requestData['url']); | |
}); | |
casper.on('resource.received', function(response) { | |
// if(response.url.indexOf('.aspx') > -1) | |
// casper.echo('Received ' + JSON.stringify(response, undefined, 4)); | |
}); | |
casper.start('http://www.bseindia.com/markets/equity/EQReports/MarketWatch.aspx', function(){ | |
var self = this; | |
this.page.onResourceReceived = function(request){ | |
casper.log('Received ' + JSON.stringify(request, undefined, 4)); | |
}; | |
var postbody = this.page.evaluate(function() { | |
return $('#aspnetForm').serialize(); | |
}); | |
postbody += ['&ctl00$ContentPlaceHolder1$imgDownload.x=9','ctl00$ContentPlaceHolder1$imgDownload.y=7'].join('&'); | |
casper.echo(postbody.replace(/&/g,'\n')); | |
casper.download('http://www.bseindia.com/markets/equity/EQReports/MarketWatch.aspx', 'bsedata.csv', 'POST', postbody); | |
}); | |
casper.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment