问题
I've been trying to download a .zip file from a website using casperjs but it won't let me.
1) If I open the url with casper.thenOpen() it shows me the typical download dialog "Do you want to Open or save this file?", the thing is that I haven't been able to find a way to choose "Download it" (which is what I need) instead of "open it" with casperjs.
2) Right now I'm using casper.download() but it just downloads a 0 byte file, I think it's a better option because I can specify an address where the file is gonna be downloaded to and it doesn't ask me to download the file, it just download it which is what I need.
this is the script I'm running:
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
pageSettings: {
loadImages: true, // The WebPage instance used by Casper will
loadPlugins: false, // use these settings
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
}
});
// print out all the messages in the headless browser context
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
});
// print out all the messages in the headless browser context
casper.on("page.error", function(msg, trace) {
this.echo("Page Error: " + msg, "ERROR");
});
var url = 'http://website.com/';
casper.start(url, function() {
// search for 'casperjs' from google form
console.log("page loaded");
this.then(function(){
this.test.assertExists('form', 'form is found');
});
casper.then(function(){
this.fill('form', {
u: 'username',
p: 'password'
}, true);
this.click("#submitButton");
});
this.wait(5000);
});
casper.page.settings.webSecurityEnabled = false;
casper.waitForUrl(/affiliates/, function(){
this.echo ("start downloading");
var url = 'http://website.com/affiliates/s.ashx?c=1977';
this.download(url, '/home/enmanuel/Desktop/1977.zip');
this.echo("finish download");
});
casper.then(function(){this.wait(5000);});
casper.run();
I'm running the script like:
casperjs UnionSquare.js --engine=slimerjs --disk-cache=no
Right now I'm getting this errors:
[error] [remote] getBinary(): Error while fetching : [Exception... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: /usr/local/lib/node_modules/casperjs/modules/clientutils.js :: sendAJAX :: line 894" data: no]
UPDATE
After a while looking for answers I found that it wasn't a casper limitation, it's an engine limitation, here the current status of the issue
If you want to make something similar to this, I ended up doing it with pure nodejs and some modules Here is how I did it
来源:https://stackoverflow.com/questions/40342285/unable-to-download-zip-file-from-url-with-casperjs