Download website to a local directory (including all css, images, js, etc.)
Note: by default dynamic websites (where content is loaded by js) may be saved not correctly because
website-scraper doesn't execute js, it only parses http responses for html and css files. If you need to download dynamic website take a look on website-scraper-phantom.
npm install website-scraper
var scrape = ;var options =urls: ''directory: '/path/to/save/';// with promise;// or with callback;
Default options you can find in lib/config/defaults.js or get them using
Array of objects which contain urls to download and filenames for them. Required.
String, absolute path to directory where downloaded files will be saved. Directory should not exist. It will be created by scraper. Required.
Array of objects to download, specifies selectors and attribute values to select files for downloading. By default scraper tries to download all possible resources.
// Downloading images, css files and scripts;
true scraper will follow hyperlinks in html files. Don't forget to set
maxRecursiveDepth to avoid infinite downloading. Defaults to
Positive number, maximum allowed depth for hyperlinks. Other dependencies will be saved regardless of their depth. Defaults to
null - no maximum recursive depth set.
Positive number, maximum allowed depth for all dependencies. Defaults to
null - no maximum depth set.
Object, custom options for request. Allows to set cookies, userAgent, etc.
Array of objects, specifies subdirectories for file extensions. If
null all files will be saved to
/* Separate files into directories:- `img` for .jpg, .png, .svg (full path `/path/to/save/img`)- `js` for .js (full path `/path/to/save/js`)- `css` for .css (full path `/path/to/save/css`)*/;
String, filename for index page. Defaults to
Boolean, whether urls should be 'prettified', by having the
defaultFilename removed. Defaults to
true scraper will continue downloading resources after error occured, if
false - scraper will finish process and return error. Defaults to
Function which is called for each url to check whether it should be scraped. Defaults to
null - no url filter will be applied.
// Links to other websites are filtered out by the urlFiltervar scrape = ;;
String, name of one of the bundled filenameGenerators, or a custom filenameGenerator function. Filename generator determines where the scraped files are saved.
byType filenameGenerator is used the downloaded files are saved by type (as defined by the
subdirectories setting) or directly in the
directory folder, if no subdirectory is specified for the specific type.
bySiteStructure filenameGenerator is used the downloaded files are saved in
directory using same structure as on the website:
// Downloads all the crawlable files. The files are saved in the same structure as the structure of the website// Links to other websites are filtered out by the urlFiltervar scrape = ;;
Function which is called on each response, allows to customize resource or reject its downloading.
It takes 1 argument - response object of request module and should return resolved
Promise if resource should be downloaded or rejected with Error
Promise if it should be skipped.
Promise should be resolved with:
stringwhich contains response body
body(response body, string) and
metadata- everything you want to save for this resource (like headers, original text, timestamps, etc.), scraper will not use this field at all, it is only for result.
// Rejecting resources with 404 status and adding metadata to other resources;
Scrape function resolves with array of Resource objects which contain
metadata property from
Class which saves Resources, should have methods
errorCleanup which return Promises. Use it to save files where you need: to dropbox, amazon S3, existing directory, etc. By default all files are saved in local file system to new directory passed in
directory option (see lib/resource-saver/index.js).
Function called each time when resource is saved to file system. Callback is called with Resource object. Defaults to
null - no callback will be called.
Function called each time when resource's downloading/handling/saving to fs was failed. Callback is called with - Resource object and
Error object. Defaults to
null - no callback will be called.
Callback function, optional, includes following parameters:
error: if error -
Errorobject, if success -
result: if error -
null, if success - array of Resource objects containing:
url: url of loaded page
filename: filename where page was saved (relative to
children: array of children Resources
This module uses debug to log events. To enable logs you should use environment variable
Next command will log everything from website-scraper
export DEBUG=website-scraper*; node app.js
Module has different loggers for levels:
website-scraper:log. Please read debug documentation to find how to include/exclude specific loggers.