Posted by SF on Thu 28th Jun 15:59
download | new post | report as spam

  1. //Load Modules
  2. phantom.casperPath = 'C:\\Development\\PhantomJS\\casper';
  3. phantom.injectJs(phantom.casperPath + '\\bin\\bootstrap.js');
  4. var system = require('system');
  5. var casper = require('casper').create({
  6.         loadImages: true,
  7.     loadPlugins: true,
  8.     verbose: true,
  9.         clientScripts: ['C:\\Development\\PhantomJS\\jquery.js'],
  10.         viewportSize: {
  11.         width: 1366,
  12.         height: 768,
  13.     },
  14.     pageSettings: {
  15.         javascriptEnabled: true,
  16.                 localToRemoteUrlAccessEnabled: true,
  17.         userAgent: 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5',
  18.                 XSSAuditingEnabled: false
  19.     }
  20. });
  21.  
  22. //Check Params
  23. if (system.args.length < 4 || system.args.length > 4) {
  24.     console.log('Usage: downloadimages.js Start_URL Image_Selector NextButton_Selector');
  25.     phantom.exit(1);
  26. }
  27.  
  28. //Setup
  29. var i = 0;
  30.  
  31. //Function to zero pad the image filenames
  32. function pad(number, length) {
  33.     var str = '' + number;
  34.     while (str.length < length) { str = '0' + str; }
  35.     return str;
  36. }
  37.  
  38. //Load each page
  39. casper.on('load.finished', function(status) {
  40.     this.echo(this.getTitle() + ' (' + this.getCurrentUrl() + ') loaded');
  41.        
  42.         //Attempt to download based on selector (img src)
  43.         var srcurl = this.evaluate(function(selector) {
  44.                 return $(selector).attr('src');
  45.         },{selector: system.args[2]});
  46.         if (srcurl == null) {
  47.                 this.echo('   No img src found');
  48.                
  49.                 //Attempt to download based on selector (background-image)
  50.                 var bgurl = this.evaluate(function(selector) {
  51.                         return $(selector).css('background-image').replace(/^url|[\(\)]/g, '');
  52.                 },{selector: system.args[2]});
  53.                 if (bgurl == null || bgurl == 'none') {
  54.                         this.echo('   No background-image found');
  55.                 }
  56.                 else {
  57.                         var filename = pad(i,3) + bgurl.substr(bgurl.length - 4);
  58.                         this.echo('   Found img src URL: ' + bgurl + ' , downloading as: ' + filename);
  59.                         this.download(bgurl, filename)
  60.                         i = i + 1;
  61.                 }
  62.         }
  63.         else {
  64.                 var filename = pad(i,3) + srcurl.substr(srcurl.length - 4);
  65.                 this.echo('   Found img src URL: ' + srcurl + ' , downloading as: ' + filename);
  66.                 this.download(srcurl, filename);       
  67.                 i = i + 1;
  68.         }
  69.        
  70.         //Attempt to find next button based on selector
  71.         if (this.exists(system.args[3])) {
  72.                 this.echo('   Clicking next button (30 second timeout)');
  73.                 this.click(system.args[3]);     
  74.                 //this.wait(30000);
  75.         }
  76.         else {
  77.                 this.echo('   No next button found');
  78.                 this.exit();
  79.         };
  80. });
  81.  
  82. //Load initial page
  83. console.log('Loading - ' + system.args[1]);
  84. casper.start(system.args[1]);
  85.  
  86. //GO GO GO
  87. casper.run();

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily
.
Syntax highlighting:

To highlight particular lines, prefix each line with @@
   Remember me