Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

phantomjs - casperjs download csv file

I am trying to download a csv file(advert report) from a site using the below code. The issue is, it will download the HTML page and not the csv file. I cannot give you the URL as it is behind the login, but it is similar case when you download Firefox from the below URL

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

It is a GET request and when I do inspect element Network Tab the get request gets Cancelled. I am new to Casper and don't know how to handle such requests. Any help would be appreciated

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');
});

Response Header

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Answered my own question, here is the solution

reference: https://github.com/knorrium/google-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);
        }
     }
 });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...