我一直在尝试使用casperjs从网站下载.zip文件,但它不会让我。
1)如果我用casper.thenOpen()打开网址,它会显示典型的下载对话框“你想打开还是保存这个文件?”,问题是我找不到办法选择“下载它”(这是我需要的)而不是用casperjs“打开它”。
2)现在我正在使用casper.download(),但它只是下载一个0字节的文件,我认为这是一个更好的选择,因为我可以指定一个地址,文件将被下载到它而不是请我下载文件,它只是下载它,这是我需要的。
这是我正在运行的脚本:
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();
我正在运行:
这样的脚本casperjs UnionSquare.js --engine=slimerjs --disk-cache=no
现在我收到了这个错误:
[错误] [remote] getBinary():获取时出错:[异常...“失败”nsresult:“0x80004005(NS_ERROR_FAILURE)”位置:“JS frame :: / usr / local / lib / node_modules / casperjs / modules / clientutils.js :: sendAJAX :: line 894“data:no]
经过一段时间寻找答案后,我发现这不是一个casper限制,这是一个引擎限制,here the current status of the issue
如果你想做类似的事情,我最终用纯粹的nodejs和一些模块Here is how I did it
来做