Popularity
6.6
Stable
Activity
1.3
Declining
2,110
79
354

Code Quality Rank: L5
Programming language: JavaScript
License: BSD 2-clause "Simplified" License
Tags: Miscellaneous     Queue     Simple     Cache     Eventemitter     Simplecrawler     Spider     Crawler    
Latest version: v1.1.9

simplecrawler alternatives and similar modules

Based on the "Miscellaneous" category.
Alternatively, view simplecrawler alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of simplecrawler or a related project?

Add another 'Miscellaneous' Module

README

Simple web crawler for node.js

NPM version Linux Build Status Windows Build Status Dependency Status devDependency Status Greenkeeper badge

simplecrawler is designed to provide a basic, flexible and robust API for crawling websites. It was written to archive, analyse, and search some very large websites and has happily chewed through hundreds of thousands of pages and written tens of gigabytes to disk without issue.

What does simplecrawler do?

  • Provides a very simple event driven API using EventEmitter
  • Extremely configurable base for writing your own crawler
  • Provides some simple logic for auto-detecting linked resources - which you can replace or augment
  • Automatically respects any robots.txt rules
  • Has a flexible queue system which can be frozen to disk and defrosted
  • Provides basic statistics on network performance
  • Uses buffers for fetching and managing data, preserving binary data (except when discovering links)

Documentation

Installation

npm install --save simplecrawler

Getting Started

Initializing simplecrawler is a simple process. First, you require the module and instantiate it with a single argument. You then configure the properties you like (eg. the request interval), register a few event listeners, and call the start method. Let's walk through the process!

After requiring the crawler, we create a new instance of it. We supply the constructor with a URL that indicates which domain to crawl and which resource to fetch first.

var Crawler = require("simplecrawler");

var crawler = new Crawler("http://www.example.com/");

You can initialize the crawler with or without the new operator. Being able to skip it comes in handy when you want to chain API calls.

var crawler = Crawler("http://www.example.com/")
    .on("fetchcomplete", function () {
        console.log("Fetched a resource!")
    });

By default, the crawler will only fetch resources on the same domain as that in the URL passed to the constructor. But this can be changed through the crawler.domainWhitelist property.

Now, let's configure some more things before we start crawling. Of course, you're probably wanting to ensure you don't take down your web server. Decrease the concurrency from five simultaneous requests - and increase the request interval from the default 250 ms like this:

crawler.interval = 10000; // Ten seconds
crawler.maxConcurrency = 3;

You can also define a max depth for links to fetch:

crawler.maxDepth = 1; // Only first page is fetched (with linked CSS & images)
// Or:
crawler.maxDepth = 2; // First page and discovered links from it are fetched
// Or:
crawler.maxDepth = 3; // Etc.

For a full list of configurable properties, see the configuration section.

You'll also need to set up event listeners for the events you want to listen to. crawler.fetchcomplete and crawler.complete are good places to start.

crawler.on("fetchcomplete", function(queueItem, responseBuffer, response) {
    console.log("I just received %s (%d bytes)", queueItem.url, responseBuffer.length);
    console.log("It was a resource of type %s", response.headers['content-type']);
});

Then, when you're satisfied and ready to go, start the crawler! It'll run through its queue finding linked resources on the domain to download, until it can't find any more.

crawler.start();

Events

simplecrawler's API is event driven, and there are plenty of events emitted during the different stages of the crawl.

"crawlstart"

Fired when the crawl starts. This event gives you the opportunity to adjust the crawler's configuration, since the crawl won't actually start until the next processor tick.

"discoverycomplete" (queueItem, resources)

Fired when the discovery of linked resources has completed

Param Type Description
queueItem QueueItem The queue item that represents the document for the discovered resources
resources Array An array of discovered and cleaned URL's

"invaliddomain" (queueItem)

Fired when a resource wasn't queued because of an invalid domain name

Param Type Description
queueItem QueueItem The queue item representing the disallowed URL

"fetchdisallowed" (queueItem)

Fired when a resource wasn't queued because it was disallowed by the site's robots.txt rules

Param Type Description
queueItem QueueItem The queue item representing the disallowed URL

"fetchconditionerror" (queueItem, error)

Fired when a fetch condition returns an error

Param Type Description
queueItem QueueItem The queue item that was processed when the error was encountered
error *

"fetchprevented" (queueItem, fetchCondition)

Fired when a fetch condition prevented the queueing of a URL

Param Type Description
queueItem QueueItem The queue item that didn't pass the fetch conditions
fetchCondition function The first fetch condition that returned false

"queueduplicate" (queueItem)

Fired when a new queue item was rejected because another queue item with the same URL was already in the queue

Param Type Description
queueItem QueueItem The queue item that was rejected

"queueerror" (error, queueItem)

Fired when an error was encountered while updating a queue item

Param Type Description
error QueueItem The error that was returned by the queue
queueItem QueueItem The queue item that the crawler tried to update when it encountered the error

"queueadd" (queueItem, referrer)

Fired when an item was added to the crawler's queue

Param Type Description
queueItem QueueItem The queue item that was added to the queue
referrer QueueItem The queue item representing the resource where the new queue item was found

"fetchtimeout" (queueItem, timeout)

Fired when a request times out

Param Type Description
queueItem QueueItem The queue item for which the request timed out
timeout Number The delay in milliseconds after which the request timed out

"fetchclienterror" (queueItem, error)

Fired when a request encounters an unknown error

Param Type Description
queueItem QueueItem The queue item for which the request has errored
error Object The error supplied to the error event on the request

"fetchstart" (queueItem, requestOptions)

Fired just after a request has been initiated

Param Type Description
queueItem QueueItem The queue item for which the request has been initiated
requestOptions Object The options generated for the HTTP request

"cookieerror" (queueItem, error, cookie)

Fired when an error was encountered while trying to add a cookie to the cookie jar

Param Type Description
queueItem QueueItem The queue item representing the resource that returned the cookie
error Error The error that was encountered
cookie String The Set-Cookie header value that was returned from the request

"fetchheaders" (queueItem, response)

Fired when the headers for a request have been received

Param Type Description
queueItem QueueItem The queue item for which the headers have been received
response http.IncomingMessage The http.IncomingMessage for the request's response

"downloadconditionerror" (queueItem, error)

Fired when a download condition returns an error

Param Type Description
queueItem QueueItem The queue item that was processed when the error was encountered
error *

"downloadprevented" (queueItem, response)

Fired when the downloading of a resource was prevented by a download condition

Param Type Description
queueItem QueueItem The queue item representing the resource that was halfway fetched
response http.IncomingMessage The http.IncomingMessage for the request's response

"notmodified" (queueItem, response, cacheObject)

Fired when the crawler's cache was enabled and the server responded with a 304 Not Modified status for the request

Param Type Description
queueItem QueueItem The queue item for which the request returned a 304 status
response http.IncomingMessage The http.IncomingMessage for the request's response
cacheObject CacheObject The CacheObject returned from the cache backend

"fetchredirect" (queueItem, redirectQueueItem, response)

Fired when the server returned a redirect HTTP status for the request

Param Type Description
queueItem QueueItem The queue item for which the request was redirected
redirectQueueItem QueueItem The queue item for the redirect target resource
response http.IncomingMessage The http.IncomingMessage for the request's response

"fetch404" (queueItem, response)

Fired when the server returned a 404 Not Found status for the request

Param Type Description
queueItem QueueItem The queue item for which the request returned a 404 status
response http.IncomingMessage The http.IncomingMessage for the request's response

"fetch410" (queueItem, response)

Fired when the server returned a 410 Gone status for the request

Param Type Description
queueItem QueueItem The queue item for which the request returned a 410 status
response http.IncomingMessage The http.IncomingMessage for the request's response

"fetcherror" (queueItem, response)

Fired when the server returned a status code above 400 that isn't 404 or 410

Param Type Description
queueItem QueueItem The queue item for which the request failed
response http.IncomingMessage The http.IncomingMessage for the request's response

"fetchcomplete" (queueItem, responseBody, response)

Fired when the request has completed

Param Type Description
queueItem QueueItem The queue item for which the request has completed
responseBody String \ Buffer
response http.IncomingMessage The http.IncomingMessage for the request's response

"gziperror" (queueItem, responseBody, response)

Fired when an error was encountered while unzipping the response data

Param Type Description
queueItem QueueItem The queue item for which the unzipping failed
responseBody String \ Buffer
response http.IncomingMessage The http.IncomingMessage for the request's response

"fetchdataerror" (queueItem, response)

Fired when a resource couldn't be downloaded because it exceeded the maximum allowed size

Param Type Description
queueItem QueueItem The queue item for which the request failed
response http.IncomingMessage The http.IncomingMessage for the request's response

"robotstxterror" (error)

Fired when an error was encountered while retrieving a robots.txt file

Param Type Description
error Error The error returned from getRobotsTxt

"complete"

Fired when the crawl has completed - all resources in the queue have been dealt with

A note about HTTP error conditions

By default, simplecrawler does not download the response body when it encounters an HTTP error status in the response. If you need this information, you can listen to simplecrawler's error events, and through node's native data event (response.on("data",function(chunk) {...})) you can save the information yourself.

Waiting for asynchronous event listeners

Sometimes, you might want to wait for simplecrawler to wait for you while you perform some asynchronous tasks in an event listener, instead of having it racing off and firing the complete event, halting your crawl. For example, if you're doing your own link discovery using an asynchronous library method.

simplecrawler provides a wait method you can call at any time. It is available via this from inside listeners, and on the crawler object itself. It returns a callback function.

Once you've called this method, simplecrawler will not fire the complete event until either you execute the callback it returns, or a timeout is reached (configured in crawler.listenerTTL, by default 10000 ms.)

Example asynchronous event listener

crawler.on("fetchcomplete", function(queueItem, data, res) {
    var continue = this.wait();

    doSomeDiscovery(data, function(foundURLs) {
        foundURLs.forEach(function(url) {
            crawler.queueURL(url, queueItem);
        });

        continue();
    });
});

Configuration

simplecrawler is highly configurable and there's a long list of settings you can change to adapt it to your specific needs.

crawler.initialURL : String

Controls which URL to request first

crawler.host : String

Determines what hostname the crawler should limit requests to (so long as filterByDomain is true)

crawler.interval : Number

Determines the interval at which new requests are spawned by the crawler, as long as the number of open requests is under the maxConcurrency cap.

crawler.maxConcurrency : Number

Maximum request concurrency. If necessary, simplecrawler will increase node's http agent maxSockets value to match this setting.

crawler.timeout : Number

Maximum time we'll wait for headers

crawler.listenerTTL : Number

Maximum time we'll wait for async listeners

crawler.userAgent : String

Crawler's user agent string

Default: "Node/simplecrawler <version> (https://github.com/simplecrawler/simplecrawler)"

crawler.queue : FetchQueue

Queue for requests. The crawler can use any implementation so long as it uses the same interface. The default queue is simply backed by an array.

crawler.respectRobotsTxt : Boolean

Controls whether the crawler respects the robots.txt rules of any domain. This is done both with regards to the robots.txt file, and <meta> tags that specify a nofollow value for robots. The latter only applies if the default discoverResources method is used, though.

crawler.allowInitialDomainChange : Boolean

Controls whether the crawler is allowed to change the host setting if the first response is a redirect to another domain.

crawler.decompressResponses : Boolean

Controls whether HTTP responses are automatically decompressed based on their Content-Encoding header. If true, it will also assign the appropriate Accept-Encoding header to requests.

crawler.decodeResponses : Boolean

Controls whether HTTP responses are automatically character converted to standard JavaScript strings using the iconv-lite module before emitted in the fetchcomplete event. The character encoding is interpreted from the Content-Type header firstly, and secondly from any <meta charset="xxx" /> tags.

crawler.filterByDomain : Boolean

Controls whether the crawler fetches only URL's where the hostname matches host. Unless you want to be crawling the entire internet, I would recommend leaving this on!

crawler.scanSubdomains : Boolean

Controls whether URL's that points to a subdomain of host should also be fetched.

crawler.ignoreWWWDomain : Boolean

Controls whether to treat the www subdomain as the same domain as host. So if http://example.com/example has already been fetched, http://www.example.com/example won't be fetched also.

crawler.stripWWWDomain : Boolean

Controls whether to strip the www subdomain entirely from URL's at queue item construction time.

crawler.cache : SimpleCache

Internal cache store. Must implement SimpleCache interface. You can save the site to disk using the built in file system cache like this:

crawler.cache = new Crawler.cache('pathToCacheDirectory');

crawler.useProxy : Boolean

Controls whether an HTTP proxy should be used for requests

crawler.proxyHostname : String

If useProxy is true, this setting controls what hostname to use for the proxy

crawler.proxyPort : Number

If useProxy is true, this setting controls what port to use for the proxy

crawler.proxyUser : String

If useProxy is true, this setting controls what username to use for the proxy

crawler.proxyPass : String

If useProxy is true, this setting controls what password to use for the proxy

crawler.needsAuth : Boolean

Controls whether to use HTTP Basic Auth

crawler.authUser : String

If needsAuth is true, this setting controls what username to send with HTTP Basic Auth

crawler.authPass : String

If needsAuth is true, this setting controls what password to send with HTTP Basic Auth

crawler.acceptCookies : Boolean

Controls whether to save and send cookies or not

crawler.cookies : CookieJar

The module used to store cookies

crawler.customHeaders : Object

Controls what headers (besides the default ones) to include with every request.

crawler.domainWhitelist : Array

Controls what domains the crawler is allowed to fetch from, regardless of host or filterByDomain settings.

crawler.allowedProtocols : Array.<RegExp>

Controls what protocols the crawler is allowed to fetch from

crawler.maxResourceSize : Number

Controls the maximum allowed size in bytes of resources to be fetched

Default: 16777216

crawler.supportedMimeTypes : Array.<(RegExp|string)>

Controls what mimetypes the crawler will scan for new resources. If downloadUnsupported is false, this setting will also restrict what resources are downloaded.

crawler.downloadUnsupported : Boolean

Controls whether to download resources with unsupported mimetypes (as specified by supportedMimeTypes)

crawler.urlEncoding : String

Controls what URL encoding to use. Can be either "unicode" or "iso8859"

crawler.stripQuerystring : Boolean

Controls whether to strip query string parameters from URL's at queue item construction time.

crawler.sortQueryParameters : Boolean

Controls whether to sort query string parameters from URL's at queue item construction time.

crawler.discoverRegex : Array.<(RegExp|function())>

Collection of regular expressions and functions that are applied in the default discoverResources method.

crawler.parseHTMLComments : Boolean

Controls whether the default discoverResources should scan for new resources inside of HTML comments.

crawler.parseScriptTags : Boolean

Controls whether the default discoverResources should scan for new resources inside of <script> tags.

crawler.maxDepth : Number

Controls the max depth of resources that the crawler fetches. 0 means that the crawler won't restrict requests based on depth. The initial resource, as well as manually queued resources, are at depth 1. From there, every discovered resource adds 1 to its referrer's depth.

crawler.ignoreInvalidSSL : Boolean

Controls whether to proceed anyway when the crawler encounters an invalid SSL certificate.

crawler.httpAgent : HTTPAgent

Controls what HTTP agent to use. This is useful if you want to configure eg. a SOCKS client.

crawler.httpsAgent : HTTPAgent

Controls what HTTPS agent to use. This is useful if you want to configure eg. a SOCKS client.

Fetch conditions

simplecrawler has an concept called fetch conditions that offers a flexible API for filtering discovered resources before they're put in the queue. A fetch condition is a function that takes a queue item candidate and evaluates (synchronously or asynchronously) whether it should be added to the queue or not. Please note: with the next major release, all fetch conditions will be asynchronous.

You may add as many fetch conditions as you like, and remove them at runtime. simplecrawler will evaluate every fetch condition in parallel until one is encountered that returns a falsy value. If that happens, the resource in question will not be fetched.

This API is complemented by download conditions that determine whether a resource's body data should be downloaded.

crawler.addFetchCondition(callback) โ‡’ Number

Adds a callback to the fetch conditions array. simplecrawler will evaluate all fetch conditions for every discovered URL, and if any of the fetch conditions returns a falsy value, the URL won't be queued.

Returns: Number - The index of the fetch condition in the fetch conditions array. This can later be used to remove the fetch condition.

Param Type Description
callback addFetchConditionCallback Function to be called after resource discovery that's able to prevent queueing of resource

Crawler~addFetchConditionCallback : function

Evaluated for every discovered URL to determine whether to put it in the queue.

Param Type Description
queueItem QueueItem The resource to be queued (or not)
referrerQueueItem QueueItem The resource where queueItem was discovered
callback function

crawler.removeFetchCondition(id) โ‡’ Boolean

Removes a fetch condition from the fetch conditions array.

Returns: Boolean - If the removal was successful, the method will return true. Otherwise, it will throw an error.

Param Type Description
id Number \ function

Download conditions

While fetch conditions let you determine which resources to put in the queue, download conditions offer the same kind of flexible API for determining which resources' data to download. Download conditions support both a synchronous and an asynchronous API, but with the next major release, all download conditions will be asynchronous.

Download conditions are evaluated after the headers of a resource have been downloaded, if that resource returned an HTTP status between 200 and 299. This lets you inspect the content-type and content-length headers, along with all other properties on the queue item, before deciding if you want this resource's data or not.

crawler.addDownloadCondition(callback) โ‡’ Number

Adds a callback to the download conditions array. simplecrawler will evaluate all download conditions for every fetched resource after the headers of that resource have been received. If any of the download conditions returns a falsy value, the resource data won't be downloaded.

Returns: Number - The index of the download condition in the download conditions array. This can later be used to remove the download condition.

Param Type Description
callback addDownloadConditionCallback Function to be called when the headers of the resource represented by the queue item have been downloaded

Crawler~addDownloadConditionCallback : function

Evaluated for every fetched resource after its header have been received to determine whether to fetch the resource body.

Param Type Description
queueItem QueueItem The resource to be downloaded (or not)
response http.IncomingMessage The response object as returned by node's http API
callback function

crawler.removeDownloadCondition(id) โ‡’ Boolean

Removes a download condition from the download conditions array.

Returns: Boolean - If the removal was successful, the method will return true. Otherwise, it will throw an error.

Param Type Description
id Number \ function

The queue

Like any other web crawler, simplecrawler has a queue. It can be directly accessed through crawler.queue and implements an asynchronous interface for accessing queue items and statistics. There are several methods for interacting with the queue, the simplest being crawler.queue.get, which lets you get a queue item at a specific index in the queue.

fetchQueue.get(index, callback)

Get a queue item by index

Param Type Description
index Number The index of the queue item in the queue
callback function Gets two parameters, error and queueItem. If the operation was successful, error will be null.

All queue method are in reality synchronous by default, but simplecrawler is built to be able to use different queues that implement the same interface, and those implementations can be asynchronous - which means they could eg. be backed by a database.

Manually adding to the queue

To add items to the queue, use crawler.queueURL.

crawler.queueURL(url, [referrer], [force]) โ‡’ Boolean

Queues a URL for fetching after cleaning, validating and constructing a queue item from it. If you're queueing a URL manually, use this method rather than [Crawler#queue#add](Crawler#queue#add)

Returns: Boolean - The return value used to indicate whether the URL passed all fetch conditions and robots.txt rules. With the advent of async fetch conditions, the return value will no longer take fetch conditions into account.
Emits: invaliddomain, fetchdisallowed, fetchconditionerror, fetchprevented, queueduplicate, queueerror, queueadd

Param Type Description
url String An absolute or relative URL. If relative, processURL will make it absolute to the referrer queue item.
[referrer] QueueItem The queue item representing the resource where this URL was discovered.
[force] Boolean If true, the URL will be queued regardless of whether it already exists in the queue or not.

Queue items

Because when working with simplecrawler, you'll constantly be handed queue items, it helps to know what's inside them. Here's the formal documentation of the properties that they contain.

QueueItem : Object

QueueItems represent resources in the queue that have been fetched, or will be eventually.

Properties

Name Type Description
id Number A unique ID assigned by the queue when the queue item is added
url String The complete, canonical URL of the resource
protocol String The protocol of the resource (http, https)
host String The full domain/hostname of the resource
port Number The port of the resource
path String The URL path, including the query string
uriPath String The URL path, excluding the query string
depth Number How many steps simplecrawler has taken from the initial page (which is depth 1) to this resource.
referrer String The URL of the resource where the URL of this queue item was discovered
fetched Boolean Has the request for this item been completed? You can monitor this as requests are processed.
status 'queued' \ 'spooled' \
stateData Object An object containing state data and other information about the request.
stateData.requestLatency Number The time (in ms) taken for headers to be received after the request was made.
stateData.requestTime Number The total time (in ms) taken for the request (including download time.)
stateData.downloadTime Number The total time (in ms) taken for the resource to be downloaded.
stateData.contentLength Number The length (in bytes) of the returned content. Calculated based on the content-length header.
stateData.contentType String The MIME type of the content.
stateData.code Number The HTTP status code returned for the request. Note that this code is 600 if an error occurred in the client and a fetch operation could not take place successfully.
stateData.headers Object An object containing the header information returned by the server. This is the object node returns as part of the response object.
stateData.actualDataSize Number The length (in bytes) of the returned content. Calculated based on what is actually received, not the content-length header.
stateData.sentIncorrectSize Boolean True if the data length returned by the server did not match what we were told to expect by the content-length header.

Queue statistics and reporting

First of all, the queue can provide some basic statistics about the network performance of your crawl so far. This is done live, so don't check it 30 times a second. You can test the following properties:

  • requestTime
  • requestLatency
  • downloadTime
  • contentLength
  • actualDataSize

You can get the maximum, minimum, and average values for each with the crawler.queue.max, crawler.queue.min, and crawler.queue.avg functions respectively.

fetchQueue.max(statisticName, callback)

Gets the maximum value of a stateData property from all the items in the queue. This means you can eg. get the maximum request time, download size etc.

Param Type Description
statisticName String Can be any of the strings in _allowedStatistics
callback function Gets two parameters, error and max. If the operation was successful, error will be null.

fetchQueue.min(statisticName, callback)

Gets the minimum value of a stateData property from all the items in the queue. This means you can eg. get the minimum request time, download size etc.

Param Type Description
statisticName String Can be any of the strings in _allowedStatistics
callback function Gets two parameters, error and min. If the operation was successful, error will be null.

fetchQueue.avg(statisticName, callback)

Gets the average value of a stateData property from all the items in the queue. This means you can eg. get the average request time, download size etc.

Param Type Description
statisticName String Can be any of the strings in _allowedStatistics
callback function Gets two parameters, error and avg. If the operation was successful, error will be null.

For general filtering or counting of queue items, there are two methods: crawler.queue.filterItems and crawler.queue.countItems. Both take an object comparator and a callback.

fetchQueue.filterItems(comparator, callback)

Filters and returns the items in the queue that match a selector

Param Type Description
comparator Object Comparator object used to filter items. Queue items that are returned need to match all the properties of this object.
callback function Gets two parameters, error and items. If the operation was successful, error will be null and items will be an array of QueueItems.

fetchQueue.countItems(comparator, callback, callback)

Counts the items in the queue that match a selector

Param Type Description
comparator Object Comparator object used to filter items. Queue items that are counted need to match all the properties of this object.
callback FetchQueue~countItemsCallback
callback function Gets two parameters, error and items. If the operation was successful, error will be null and items will be an array of QueueItems.

The object comparator can also contain other objects, so you may filter queue items based on properties in their stateData object as well.

crawler.queue.filterItems({
    stateData: { code: 301 }
}, function(error, items) {
    console.log("These items returned a 301 HTTP status", items);
});

Saving and reloading the queue (freeze/defrost)

It can be convenient to be able to save the crawl progress and later be able to reload it if your application fails or you need to abort the crawl for some reason. The crawler.queue.freeze and crawler.queue.defrost methods will let you do this.

A word of warning - they are not CPU friendly as they rely on JSON.parse and JSON.stringify. Use them only when you need to save the queue - don't call them after every request or your application's performance will be incredibly poor - they block like crazy. That said, using them when your crawler commences and stops is perfectly reasonable.

Note that the methods themselves are asynchronous, so if you are going to exit the process after you do the freezing, make sure you wait for callback - otherwise you'll get an empty file.

fetchQueue.freeze(filename, callback)

Writes the queue to disk in a JSON file. This file can later be imported using defrost

Param Type Description
filename String Filename passed directly to fs.writeFile
callback function Gets a single error parameter. If the operation was successful, this parameter will be null.

fetchQueue.defrost(filename, callback)

Import the queue from a frozen JSON file on disk.

Param Type Description
filename String Filename passed directly to fs.readFile
callback function Gets a single error parameter. If the operation was successful, this parameter will be null.

Cookies

simplecrawler has an internal cookie jar, which collects and resends cookies automatically and by default. If you want to turn this off, set the crawler.acceptCookies option to false. The cookie jar is accessible via crawler.cookies, and is an event emitter itself.

Cookie events

"addcookie" (cookie)

Fired when a cookie has been added to the jar

Param Type Description
cookie Cookie The cookie that has been added

"removecookie" (cookie)

Fired when one or multiple cookie have been removed from the jar

Param Type Description
cookie Array.<Cookie> The cookies that have been removed

Link Discovery

simplecrawler's discovery function is made to be replaceable โ€” you can easily write your own that discovers only the links you're interested in.

The method must accept a buffer and a queueItem, and return the resources that are to be added to the queue.

It is quite common to pair simplecrawler with a module like cheerio that can correctly parse HTML and provide a DOM like API for querying โ€” or even a whole headless browser, like phantomJS.

The example below demonstrates how one might achieve basic HTML-correct discovery of only link tags using cheerio.

crawler.discoverResources = function(buffer, queueItem) {
    var $ = cheerio.load(buffer.toString("utf8"));

    return $("a[href]").map(function () {
        return $(this).attr("href");
    }).get();
};

FAQ/Troubleshooting

There are a couple of questions that pop up more often than others in the issue tracker. If you're having trouble with simplecrawler, please have a look at the list below before submitting an issue.

  • Q: Why does simplecrawler discover so many invalid URLs?

    A: simplecrawler's built-in discovery method is purposefully naive - it's a brute force approach intended to find everything: URLs in comments, binary files, scripts, image EXIF data, inside CSS documents, and more โ€” useful for archiving and use cases where it's better to have false positives than fail to discover a resource.

    It's definitely not a solution for every case, though โ€” if you're writing a link checker or validator, you don't want erroneous 404s throwing errors. Therefore, simplecrawler allows you to tune discovery in a few key ways:

    • You can either add to (or remove from) the crawler.discoverRegex array, tweaking the search patterns to meet your requirements; or
    • Swap out the discoverResources method. Parsing HTML pages is beyond the scope of simplecrawler, but it is very common to combine simplecrawler with a module like cheerio for more sophisticated resource discovery.

    Further documentation is available in the link discovery section.

  • Q: Why did simplecrawler complete without fetching any resources?

    A: When this happens, it is usually because the initial request was redirected to a different domain that wasn't in the crawler.domainWhitelist.

  • Q: How do I crawl a site that requires a login?

    A: Logging in to a site is usually fairly simple and most login procedures look alike. We've included an example that covers a lot of situations, but sadly, there isn't a one true solution for how to deal with logins, so there's no guarantee that this code works right off the bat.

    What we do here is:

    1. fetch the login page,
    2. store the session cookie assigned to us by the server,
    3. extract any CSRF tokens or similar parameters required when logging in,
    4. submit the login credentials.
    var Crawler = require("simplecrawler"),
        url = require("url"),
        cheerio = require("cheerio"),
        request = require("request");
    
    var initialURL = "https://example.com/";
    
    var crawler = new Crawler(initialURL);
    
    request("https://example.com/login", {
        // The jar option isn't necessary for simplecrawler integration, but it's
        // the easiest way to have request remember the session cookie between this
        // request and the next
        jar: true
    }, function (error, response, body) {
        // Start by saving the cookies. We'll likely be assigned a session cookie
        // straight off the bat, and then the server will remember the fact that
        // this session is logged in as user "iamauser" after we've successfully
        // logged in
        crawler.cookies.addFromHeaders(response.headers["set-cookie"]);
    
        // We want to get the names and values of all relevant inputs on the page,
        // so that any CSRF tokens or similar things are included in the POST
        // request
        var $ = cheerio.load(body),
            formDefaults = {},
            // You should adapt these selectors so that they target the
            // appropriate form and inputs
            formAction = $("#login").attr("action"),
            loginInputs = $("input");
    
        // We loop over the input elements and extract their names and values so
        // that we can include them in the login POST request
        loginInputs.each(function(i, input) {
            var inputName = $(input).attr("name"),
                inputValue = $(input).val();
    
            formDefaults[inputName] = inputValue;
        });
    
        // Time for the login request!
        request.post(url.resolve(initialURL, formAction), {
            // We can't be sure that all of the input fields have a correct default
            // value. Maybe the user has to tick a checkbox or something similar in
            // order to log in. This is something you have to find this out manually
            // by logging in to the site in your browser and inspecting in the
            // network panel of your favorite dev tools what parameters are included
            // in the request.
            form: Object.assign(formDefaults, {
                username: "iamauser",
                password: "supersecretpw"
            }),
            // We want to include the saved cookies from the last request in this
            // one as well
            jar: true
        }, function (error, response, body) {
            // That should do it! We're now ready to start the crawler
            crawler.start();
        });
    });
    
    crawler.on("fetchcomplete", function (queueItem, responseBuffer, response) {
        console.log("Fetched", queueItem.url, responseBuffer.toString());
    });
    
  • Q: What does it mean that events are asynchronous?

    A: One of the core concepts of node.js is its asynchronous nature. I/O operations (like network requests) take place outside of the main thread (which is where your code is executed). This is what makes node fast, the fact that it can continue executing code while there are multiple HTTP requests in flight, for example. But to be able to get back the result of the HTTP request, we need to register a function that will be called when the result is ready. This is what asynchronous means in node - the fact that code can continue executing while I/O operations are in progress - and it's the same concept as with AJAX requests in the browser.

  • Q: Promises are nice, can I use them with simplecrawler?

    A: No, not really. Promises are meant as a replacement for callbacks, but simplecrawler is event driven, not callback driven. Using callbacks to any greater extent in simplecrawler wouldn't make much sense, since you normally need to react more than once to what happens in simplecrawler.

  • Q: Something's happening and I don't see the output I'm expecting!

    Before filing an issue, check to see that you're not just missing something by logging all crawler events with the code below:

    var originalEmit = crawler.emit;
    crawler.emit = function(evtName, queueItem) {
        crawler.queue.countItems({ fetched: true }, function(err, completeCount) {
            if (err) {
                throw err;
            }
    
            crawler.queue.getLength(function(err, length) {
                if (err) {
                    throw err;
                }
    
                console.log("fetched %d of %d โ€” %d open requests, %d open listeners",
                    completeCount,
                    length,
                    crawler._openRequests.length,
                    crawler._openListeners);
            });
        });
    
        console.log(evtName, queueItem ? queueItem.url ? queueItem.url : queueItem : null);
        originalEmit.apply(crawler, arguments);
    };
    

    If you don't see what you need after inserting that code block, and you still need help, please attach the output of all the events fired with your email/issue.

Node Support Policy

Simplecrawler will officially support stable and LTS versions of Node which are currently supported by the Node Foundation.

Currently supported versions:

  • 8.x
  • 10.x
  • 12.x

Current Maintainers

Contributing

Please see the contributor guidelines before submitting a pull request to ensure that your contribution is able to be accepted quickly and easily!

Contributors

simplecrawler has benefited from the kind efforts of dozens of contributors, to whom we are incredibly grateful. We originally listed their individual contributions but it became pretty unwieldy - the full list can be found here.

License

Copyright (c) 2017, Christopher Giffard.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*Note that all licence references and agreements mentioned in the simplecrawler README section above are relevant to that project's source code only.