Casperjs下载CSV文件[英] casperjs download csv file

问题描述

我正在尝试使用以下代码从站点下载CSV文件(广告报告).问题是,它将下载HTML页面,而不是CSV文件.我不能为您提供登录后的URL,但是当您从下面的URL下载Firefox时,这是类似的情况.

http://www.mozilla.org/en-us/firefox/new/

这是一个get请求,当我检查元素网络选项卡时,get请求被取消.我是卡斯珀(Casper)的新手,不知道如何处理此类请求.任何帮助将不胜感激

casper.then(function() {
   var downloadURL = "";

  this.evaluate(function() {
      var downloadURL = "http://www.lijit.com"+jQuery("#dailyCSV").attr('href'); 
   });

   this.download(downloadURL, '/Users/Ujwal/Downloads/caspertests/stats.csv');
});

响应标头

Age:0
Cache-Control:max-age=0
Connection:keep-alive
Content-Disposition:attachment; filename=stats.csv
Content-Encoding:gzip
Content-Length:1634
Content-Type:text/x-csv
Date:Sat, 05 Oct 2013 15:28:21 GMT
Expires:Sat, 05 Oct 2013 15:28:21 GMT
P3P:CP="CUR ADM OUR NOR STA NID"
Server:PWS/8.0.16
Vary:Accept-Encoding
X-Px:ms h0-s28.p9-jfk ( h0-s62.p9-jfk), ms h0-s62.p9-jfk ( origin>CONN)

推荐答案

回答了我自己的问题,这是解决方案

参考: https://github.com/github.com/knorrium/google-google-bookle-books-downloader/blob/master/gbd.js

 //Download the daily csv
 casper.then(function() {    
     this.click('#dailyCSV');
 });

 casper.on('resource.received', function (resource) {
     "use strict";
     if ((resource.url.indexOf("publisherCSV/?startDate=") !== -1) ) {        
        this.echo(resource.url);
        var url, file;
        url = resource.url;
        file = "stats.csv";
        try {
            this.echo("Attempting to download file " + file);
            var fs = require('fs');
            casper.download(resource.url, fs.workingDirectory+'/'+file);
        } catch (e) {
            this.echo(e);
        }
     }
 });

本文地址:https://www.itbaoku.cn/post/1739823.html