Casperjs Hello World in 10 Minutes

This article represents instructions on how to get started with programming casperjs scripts within 10 minutes. In this article, the sample program is written for scraping Amazon.com homepage and printing title of the page.

One of the biggest issues or hurdle, I would say, in getting started with any framework is related with downloading the tool/library, installing and configuring the environment in order to execute the program. Thanks to Docker creators, this is taken care in a very neat manner so much so that one could do following to set up the development environment in literally no time.

  1. Download the image of the development environment.
  2. Create a Docker container using that image in detached mode
  3. Start and attach with the container anytime one want to code and execute the program

Docker Container for Casperjs

For those of you not aware of what are dockers, do check their website, http://www.docker.com. Also, check my previous article on setting up docker on your windows box. In order to get started with Casperjs, first step is to download the image of Casperjs container from following page.

https://github.com/philalex/docker-casperjs

Once the zip file gets downloaded, unzip the file, goto that folder and run following commands to create a container:

# Pull the docker image
docker pull philalex/docker-casperjs
# Command to run the container with image philalex/docker-casperjs
docker run -ti -dP --name casper1 philalex/docker-casperjs

Alternatively, you could just run the following command. It checks if an image is present and the runs the container using that image. In case the image is not present, it downloads the image first and then runs the container.

# Command to run the container with image philalex/docker-casperjs
docker run -ti -dP --name casper1 philalex/docker-casperjs

Once the container is created, execute the following command to start and attach the container:

# Start the container
docker start casper1
# Attach the container
docker attach casper1

Once the container gets started, do the following in the /home folder.

  1. Create the following Casperjs file, namely sample.js
    var casper = require('casper').create();
    casper.start('http://www.amazon.com', function() {
        this.echo(this.getTitle());
    });
    casper.run();
     
    
  2. Run the following command. The title of amazon.com page is printed.
    casperjs sample.js
    
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com

Share
Published by
Ajitesh Kumar

Recent Posts

Logistic Regression in Machine Learning: Python Example

Last updated: 26th April, 2024 In this blog post, we will discuss the logistic regression…

20 hours ago

MSE vs RMSE vs MAE vs MAPE vs R-Squared: When to Use?

Last updated: 22nd April, 2024 As data scientists, we navigate a sea of metrics to…

2 days ago

Gradient Descent in Machine Learning: Python Examples

Last updated: 22nd April, 2024 This post will teach you about the gradient descent algorithm…

5 days ago

Loss Function vs Cost Function vs Objective Function: Examples

Last updated: 19th April, 2024 Among the terminologies used in training machine learning models, the…

1 week ago

Model Parallelism vs Data Parallelism: Examples

Last updated: 19th April, 2024 Model parallelism and data parallelism are two strategies used to…

1 week ago

Model Complexity & Overfitting in Machine Learning: How to Reduce

Last updated: 4th April, 2024 In machine learning, model complexity, and overfitting are related in…

2 weeks ago