website-scraper
Options | Plugins | Security | Log and debug | Frequently Asked Questions | Contributing | Code of Conduct
Download the website to the local directory (including all css, images, js, etc.)
Note: by default dynamic websites (where content is loaded by js) may not be saved correctly because website-scraper doesn't execute js, it only parses http responses for html and css files. If you need to download a dynamic website take a look at website-scraper-puppeteer.
Sponsors
Maintenance of this project is made possible by all the contributors and sponsors.
If you'd like to sponsor this project and have your avatar or company logo appear below click here. 💖







Requirements
Installation
npm install website-scraper
Usage
import scrape from 'website-scraper'; // only as ESM, no CommonJS
const options = {
urls: ['http://nodejs.org/'],
directory: '/path/to/save/'
};
// with async/await
const result = await scrape(options);
// with promise
scrape(options).then((result) => {});
options
Default options you can find in lib/config/defaults.js or get them using
import defaultOptions from 'website-scraper/defaultOptions';
urls
Array of objects which contain urls to download and filenames for them. Required.
scrape({
urls: [
'http://nodejs.org/', // Will be saved with default filename 'index.html'
{url: 'http://nodejs.org/about', filename: 'about.html'},
{url: 'http://blog.nodejs.org/', filename: 'blog.html'}
],
directory: '/path/to/save'
});
directory
String, absolute path to directory where downloaded files will be saved. Directory should not exist. It will be created by scraper. Required.
How to download website to existing directory and why it's not supported by default - check here.
sources
Array of objects to download, specifies selectors and attribute values to select files for downloading. By default scraper tries to download all possible resources. Scraper uses cheerio to select html elements so selector can be any selector that cheerio supports.
// Downloading images, css files and scripts
scrape({
urls: ['http://nodejs.org/'],
directory: '/path/to/save',
sources: [
{selector: 'img', attr: 'src'},
{selector: 'link[rel="stylesheet"]', attr: 'href'},
{selector: 'script', attr: 'src'}
]
});
recursive
Boolean, if true scraper will follow hyperlinks in html files. Don't forget to set maxRecursiveDepth to avoid infinite downloading. Defaults to false.
maxRecursiveDepth
Positive number, maximum allowed depth for hyperlinks. Other dependencies will be saved regardless of their depth. Defaults to null - no maximum recursive depth set.
maxDepth
Positive number, maximum allowed depth for all dependencies. Defaults to null - no maximum depth set.
In most of cases you need maxRecursiveDepth instead of this option.
The difference between maxRecursiveDepth and maxDepth is that
maxDepth is for all type of resources, so if you have
maxDepth=1 AND html (depth 0) ⟶ html (depth 1) ⟶ img (depth 2)
last image will be filtered out by depth
maxRecursiveDepth is only for html resources, so if you have
maxRecursiveDepth=1 AND html (depth 0) ⟶ html (depth 1) ⟶ img (depth 2)
only html resources with depth 2 will be filtered out, last image will be downloaded
request
Object, custom options for http module got which is used inside website-scraper. Allows to set retries, cookies, userAgent, encoding, etc.
// use same request options for all resources
scrape({
urls: ['http://example.com/'],
directory: '/path/to/save',
request: {
headers: {
'User-Agent': 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19'
}
}
});
subdirectories
Array of objects, specifies subdirectories for file extensions. If null all files will be saved to directory.
/* 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`)
*/
scrape({
urls: ['http://example.com'],
directory: '/path/to/save',
subdirectories: [
{directory: 'img', extensions: ['.jpg', '.png', '.svg']},
{directory: 'js', extensions: ['.js']},
{directory: 'css', extensions: ['.css']}
]
});
defaultFilename
String, filename for index page. Defaults to index.html.
prettifyUrls
Boolean, whether urls should be 'prettified', by having the defaultFilename removed. Defaults to false.
ignoreErrors
Boolean, if true scraper will continue downloading resources after error occurred, if false - scraper will finish process and return error. Defaults to false.
Errors thrown by plugin actions while handling a resource (including onResourceSaved) are treated the same way.
urlFilter
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 urlFilter
scrape({
urls: ['http://example.com/'],
urlFilter: function(url) {
return url.indexOf('http://example.com') === 0;
},
directory: '/path/to/save'
});
urlFilter is called for every url found in downloaded content and, when such a resource is redirected, for the url it was redirected to. It is not called for the urls passed in urls, validate those before calling scrape. See Security for how to restrict destinations for all requests.
filenameGenerator
String (name of the bundled filenameGenerator). Filename generator determines path in file system where the resource will be saved.
byType (default)
When the byType filenameGenerator is used the downloaded files are saved by extension (as defined by the subdirectories setting) or directly in the directory folder, if no subdirectory is specified for the specific extension.
bySiteStructure
When the bySiteStructure filenameGenerator is used the downloaded files are saved in directory using same structure as on the website:
/ => DIRECTORY/example.com/index.html
/about => DIRECTORY/example.com/about/index.html
//cdn.example.com/resources/jquery.min.js => DIRECTORY/cdn.example.com/resources/jquery.min.js
scrape({
urls: ['http://example.com/'],
urlFilter: (url) => url.startsWith('http://example.com'), // Filter links to other websites
recursive: true,
maxRecursiveDepth: 10,
filenameGenerator: 'bySiteStructure',
directory: '/path/to/save'
});
requestConcurrency
Number, maximum amount of concurrent requests. Defaults to Infinity.
plugins
Plugins allow to extend scraper behaviour
Built-in plugins
Scraper has built-in plugins which are used by default if not overwritten with custom plugins. You can find them in lib/plugins directory. These plugins are intended for internal use but can be copied if the behaviour of the plugins needs to be extended / changed.
Existing plugins
Create plugin
Plugin is object with .apply method, can be used to change scraper behavior.
.apply method takes one argument - registerAction function which allows to add handlers for different actions. Action handlers are functions that are called by scraper on different stages of downloading website. For example generateFilename is called to generate filename for resource based on its url, onResourceError is called when error occurred during requesting/handling/saving resource.