MoneyBag
MoneyBag

Reputation: 113

Pass URL to CasperJS via CLI

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.

Upvotes: 4

Views: 4687

Answers (3)

Fanch
Fanch

Reputation: 3274

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(......)

Upvotes: 6

MoneyBag
MoneyBag

Reputation: 113

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

Upvotes: 4

Paritosh Piplewar
Paritosh Piplewar

Reputation: 8124

try this

  var url = casper.cli.get(0)

Upvotes: 4

Related Questions