2013-10-05 11 views
9

Próbuję pobrać plik csv (raport z ogłoszenia) z witryny, używając poniższego kodu. Problem polega na tym, że pobierze stronę HTML, a nie plik csv. Nie mogę dać ci adres URL, ponieważ jest za logowaniu, ale jest podobny przypadek podczas pobierania Firefoksa z poniższym URLPobierz plik CSV dla CasSci

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

Jest to żądanie GET i kiedy zrobić inspekcję elementu Zakładka Network get żądanie zostanie anulowane. Jestem nowy w Casper i nie wiem, jak sobie z nimi radzić. Każda pomoc będzie mile widziane

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 żniwna

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) 

Odpowiedz

16

odpowiedział na moje własne pytanie, o to rozwiązanie

referencyjny: 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); 
     } 
    } 
});