Casperjs下载帖子附件
Casperjs Download Post Attachment
我发现了这个问题,然后在PhantomJs中下载了一个作为POST请求响应附件的文件。但它保存的文件是一个html文件,上面写着404 - File or directory not found.
下载这个csv文件时我还缺少什么吗?
以下是的响应标头
Cache-Control:private
Content-Disposition:attachment;filename=LenderCompetitionReport.csv
Content-Length:249487
Content-Type:application/csv
Date:Tue, 08 Dec 2015 14:32:47 GMT
Pragma:
Server:Microsoft-IIS/7.0
Set-Cookie:NSC_JOy4ob05etjhas0d2kv5wzcdlqr2zbu=ffffffff09149e4f45525d5f4f58455e445a4a423660;expires=Tue, 08-Dec-2015 14:34:47 GMT;path=/;secure;httponly
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
这是我的代码
casper.waitForSelector(x("//*[@id='ContentBody_ContentBody_btnExport']"),function() {
casper.then(function() {
var res = this.page.evaluate(function() {
var res={};
f=document.forms["frmBase"];
f.onsubmit= function() {
//iterate the form fields
var post={};
for(i=0; i<f.elements.length; i++) {
post[f.elements[i].name]=f.elements[i].value;
}
res.action = f.action;
res.post = post;
return false; //Stop form submission
}
//Trigger the click on the link.
var l = $("#ContentBody_BodyLevelElment_lbtnCsv");
l.click();
return res; //Return the form data to casper
});
//Start the download
casper.download(res.action, "LenderCompetitionReport.csv", "POST", res.post);
});
});
我最终使用Selenium来下载文件,因为看起来网站已经实现了某种安全性,不允许我在原始请求后之外下载它
使用phatomjs下载任何报告文件。我使用的是phatomjs2.0 for windowsRun如下命令phatomjs--cookie文件=D:''xamplep''htdocs''automation''cookies.txt D:''xampp''htdocs''automation''fetch_example_report.js 2018-12-24T14:4:04+000:00 2018-12-23T14:44+000:00
var page = require('webpage').create();
var fs = require('fs');
var system = require('system');
var total_arg = system.args;
var uDateTime = total_arg[1];
var pDateTime = total_arg[2];
phantom.cookiesEnabled = true;
var tp = phantom.cookies;
page.open('https://example.com/login.php',function(status) {
if (status === "success") {
page.includeJs('https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js', function() {
page.evaluate(function() {
$('input[name=email]').val('example@example.com');
$('input[name=userPass]').val('password');
$('input[type=submit]').click();
});
});
window.setTimeout(function() {
page.open('https://example.com/daily_report.php', function(status) {
console.log(uDateTime);
var phantom_d = new Date();
console.log(phantom_d);
var ln=phantom_d.getUTCMonth().toString;
var phantom_p =new Date(pDateTime);
var ln_p=phantom_p.getUTCMonth().toString;
var phantom_month=(ln.length==1)? 0+(phantom_d.getUTCMonth()+1).toString(): (phantom_d.getUTCMonth()+1);
var phantom_currentDate = phantom_d.getUTCFullYear()+'-'+phantom_month+'-'+phantom_d.getUTCDate();
if(phantom_d.getUTCDate() -1 ==0){
var phantom_month_p=(ln_p.length==1)? 0+(phantom_p.getUTCMonth()+1).toString(): (phantom_p.getUTCMonth()+1);
var phantom_previousDate = phantom_p.getUTCFullYear()+'-'+phantom_month_p+'-'+(phantom_p.getUTCDate());
}else{
var phantom_previousDate = phantom_d.getUTCFullYear()+'-'+phantom_month+'-'+(phantom_d.getUTCDate() -1);
}
var myarray =["m.ID", "email", "custNum", "sForce", "store", "storeType", "orderDate", "fullname", "companyName", "shipFirstname", "shipLastname", "shipAddress1","cancel_code"];
console.log(phantom_previousDate);
var res= page.evaluate(function(cdate,pdate, my_fieldarray) {
$("#fields").val(my_fieldarray);
$("select[name=ord_status]").val('30');
$('input[name=toDate]').val(cdate);
$('input[name=fromDate]').val(pdate);
$("input[name=order_distinct]").click();
$('input[name=export]').click();
},phantom_currentDate,phan`enter code here`tom_previousDate, myarray);
window.setTimeout(function() {
phantom.exit();
}, 5000);
});
}, 8000);
}
});
page.onConsoleMessage = function(msg) {
console.log(msg);
}
page.onFileDownload = function(status){ //window.setTimeout(function() {
console.log('onFileDownload(' + status + ')');
var phantom_d = new Date(uDateTime);
var ln_hour=phantom_d.getUTCHours().toString();
var phantom_hour=(ln_hour.length==1)? 0+ln_hour: ln_hour;
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var fileName = phantom_d.getUTCFullYear()+'-'+months[phantom_d.getUTCMonth()]+'-'+phantom_d.getUTCDate()+'-'+phantom_hour+'.xls';
return 'D:'xampp'htdocs'automation''+fileName;
}
page.onResourceReceived = function(status){
//console.log('onResourceReceived(' + status.stage + ')');
if(status.stage === 'end'){ //phantom.exit(1);
}
}
page.onResourceRequested = function(status){
//console.log('onResourceRequested(' + status + ')');
}
page.onFileDownloadError = function(status){
//console.log('onFileDownloadError(' + status + ')');//phantom.exit(1);
}
page.onLoadStarted = function(status){
//console.log('onLoadStarted(' + status + ')');
}
page.onLoadFinished = function(status){
//console.log('onLoadFinished(' + status + ')');
//document.getElementById('qua').textContent;
}
相关文章:
- 如何在生成下载文件时显示加载动画
- 直接下载文件,而不是从window.open(url)
- 如何使用javascript或html下载PDF格式的填写表单
- Javascript运行php文件,然后下载文件
- 在单击href链接的同时下载文件
- 尽管链接成功并已成功下载,但未找到NPM模块
- 通过php页面中的js强制下载txt
- CasperJS在使用sendKeys时不会上传文件
- 使用angularjs向浏览器发送servlet响应(下载功能)
- 通过javascript下载文件时设置文件名
- 如何在Edge中下载图像/png数据URI
- 下载使用POST数据动态生成的文件
- CasperJS-下载生成的没有URL的文件
- 如何使用 casperjs 下载与我在浏览器中看到的页面相同的页面
- 下载基于 Content-Type JavaScript Casperjs 的链接
- CasperJS下载并将文件保存到特定位置
- Casperjs下载帖子附件
- 我们如何使用CasperJS实现基于浏览器的下载窗口的自动化
- 如何使用casperjs下载没有url的文件
- 单击按钮以使用CasperJS下载文件