4

I'm using CasperJS to evaluate a webpage. What I would like to do is to let me pass an argument that is a URL, have CasperJS download and evaluate the page, and output to standard out the webpage so I can use it in a BaSH script. Here is my code so far for Casper:

var casper = require('casper').create();
var url = casper.cli.args;

casper.start(url, function() {
    this.evaluate(function() {
        return document;
    });
    this.echo(this.getHTML());
});
casper.run();

This is what I'm seeing once I run it:

@:~/spider/casperjs$ casperjs viewsource.js google.com
CasperError: No steps defined, aborting                                         
  /usr/local/src/casperjs/modules/casper.js:1510 in run
  ~/spider/casperjs/viewsource.js:10

Help please.

Brice Favre
  • 1,511
  • 1
  • 15
  • 34
MoneyBag
  • 113
  • 1
  • 7
  • Possible duplicate of [How to pass a variable to a CasperJS script through the command line?](http://stackoverflow.com/questions/21765178/how-to-pass-a-variable-to-a-casperjs-script-through-the-command-line) – Ciro Santilli OurBigBook.com Nov 17 '15 at 21:48

3 Answers3

6

If you want to name your argument :

command :

casperjs viewsource.js --url="http://YourUrl.com"

script :

var mainUrl = casper.cli.get("url");

casper.start(mainUrl)
.then(......)
Fanch
  • 3,274
  • 3
  • 20
  • 51
  • May be, a small help from my end, if you want to validate the above code with this additional snippet: `if (!casper.cli.has("url")) { casper.echo("\nUsage:casperjs viewsource.js --url=http://YourUrl.com/").exit(); } ` This would ensure that your below code executes, only if --url is passed in CLI argument list. Hope this helps. – moronkreacionz Dec 14 '15 at 13:49
4

try this

  var url = casper.cli.get(0)
Paritosh Piplewar
  • 7,982
  • 5
  • 26
  • 41
  • i get @:~/spider/casperjs$ casperjs viewsource.js google.com – MoneyBag Mar 23 '14 at 04:23
  • 1
    So I fixed my script and now it returns the URL twice and does not exit. here is the script: var casper = require('casper').create(); var url = casper.cli.get(0); casper.start(url, function () { this.echo(url); }); casper.run(function() { this.echo(url).exit; }); – MoneyBag Mar 23 '14 at 05:03
4

I finally got it. here is the script:

var casper = require('casper').create();
var url = casper.cli.get(0);

casper.start(url, function () {
    this.evaluate(function() {
        return document;
    });
    this.echo(this.getHTML());
});
casper.run(function() {
    this.exit();
});
MoneyBag
  • 113
  • 1
  • 7