Introduction and Installation of Nightmare.js
Last Updated :
19 Oct, 2021
Nightmare is provided by Segment and is a high-level browser automation library. The goal for this Nightmare is to follow the path of the user actions (like goto, type, click, etc.), in order to expose few simple methods with an API that feels synchronous for each block of scripting, rather than deeply nested callbacks. It has been designed for automating tasks across sites that don’t have APIs, but nowadays is most often used for crawling, UI testing, etc.
Note: Nightmare is intended to be run on NodeJS 4.x or higher.
Alternatives to Nightmare.js: There are many alternatives for this, Some of the most common alternatives of Nightmate.js are:
- Puppeteer
- PhantomJS
- fancybox
- MomentJS
- Lodash
- Underscore
Project Setup and installing required modules:
-
Step 1: Install Nightmare.js and Jquery module using the following command:
npm install nightmare
npm install jquery
-
Step 2: Create an index.js file and require the module using the following way:
const Nightmare = require('nightmare');
nightmare = Nightmare({ show: true});
This will create an instance of Nightmare which can navigate to the web. It is possible to put optional parameters in the JSON passed inside the Nightmare() as shown above.
Project Structure: It will look like the following.
Example: Let us see an example to use Nightmare.js for CraigLists scrapping:
Filename: index.js
index.js
var jquery = require( 'jquery' )
var Nightmare = require( 'nightmare' ),
nightmare = Nightmare()
var city = process.argv[2]
'.craigslist.org/search/cpg?is_paid=yes&postedToday=1' )
.wait(2000)
.evaluate( function () {
var gigs = [];
$( '.hdrlnk' ).each( function () {
item = {}
item[ "title" ] = $( this ).text()
item[ "link" ] = $( this ).attr( "href" )
gigs.push(item)
})
return gigs
})
.end()
.then( function (result) {
for (gig in result) {
console.log(result[gig].title);
}
})
|
Run the index.js file using the following command:
node index.js cityname
For example, we can search for saltlakecity as shown below:
node index.js saltlakecity
Output: Wait for few seconds, and you will get the output.
Share your thoughts in the comments
Please Login to comment...