Casperjs下载帖子附件

时间:2015-12-08 15:33:06

标签: javascript casperjs

我发现了这个问题,然后跟着downloading a file that comes as an attachment in a POST request response in PhantomJs。但它保存的文件是一个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);
            });
    });

2 个答案:

答案 0 :(得分:0)

我最后使用Selenium来下载该文件,因为看起来网站已经实现了某种安全性,但我不允许我在原始帖子请求之外下载它

答案 1 :(得分:0)

使用phantomjs下载任何报告文件。我正在使用phantomjs 2.0 for windows在以下命令中运行 phatomjs --cookies文件= D:\ xampp \ htdocs \ automation \ cookies.txt D:\ xampp \ htdocs \ automation \ fetch_example_report.js 2018-12-24T14:44:04 + 00:00 2018-12-23T14: 44:04 + 00: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;
}