Gathering detailed insights and metrics for crawler
Gathering detailed insights and metrics for crawler
Gathering detailed insights and metrics for crawler
Gathering detailed insights and metrics for crawler
Crawler
Search for anything on web.
@nodelib/fs.walk
A library for efficiently walking a directory recursively
fdir
The fastest directory crawler & globbing alternative to glob, fast-glob, & tiny-glob. Crawls 1m files in < 1s
npm-license-crawler
Analyzes license information for multiple node.js modules (package.json files) as part of your software project.
npm install crawler
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (40.87%)
JavaScript (37.82%)
HTML (21.3%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
6,766 Stars
598 Commits
879 Forks
254 Watchers
4 Branches
56 Contributors
Updated on Jul 10, 2025
Latest Version
2.0.2
Package Id
crawler@2.0.2
Unpacked Size
104.19 kB
Size
26.77 kB
File Count
55
NPM Version
10.7.0
Node Version
18.20.3
Published on
Jul 16, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Crawler v2 : Advanced and Typescript version of node-crawler
Features:
If you have prior experience with Crawler v1, for fast migration, please proceed to the section Differences and Breaking Changes.
Requires Node.js 18 or above.
IMPORTANT: If you are using a Linux OS, we currently recommend sticking with Node.js version 18 for the time being, rather than opting for higher versions (even if some dependencies suggest 20 or later). Our unit tests have encountered stability issues on Linux with higher versions of Node.js, which may be caused by more profound underlying reasons. However, at present, we do not have the resources to address these issues.
1$ npm install crawler
Warning: Given the dependencies involved (Especially migrating from request to got) , Crawler v2 has been designed as a native ESM and no longer offers a CommonJS export. We would also like to recommend that you convert to ESM. Note that making this transition is generally not too difficult.
1import Crawler from "crawler"; 2 3const c = new Crawler({ 4 maxConnections: 10, 5 // This will be called for each crawled page 6 callback: (error, res, done) => { 7 if (error) { 8 console.log(error); 9 } else { 10 const $ = res.$; 11 // $ is Cheerio by default 12 //a lean implementation of core jQuery designed specifically for the server 13 console.log($("title").text()); 14 } 15 done(); 16 }, 17}); 18 19// Add just one URL to queue, with default callback 20c.add("http://www.amazon.com"); 21 22// Add a list of URLs 23c.add(["http://www.google.com/", "http://www.yahoo.com"]); 24 25// Add URLs with custom callbacks & parameters 26c.add([ 27 { 28 url: "http://parishackers.org/", 29 jQuery: false, 30 31 // The global callback won't be called 32 callback: (error, res, done) => { 33 if (error) { 34 console.log(error); 35 } else { 36 console.log("Grabbed", res.body.length, "bytes"); 37 } 38 done(); 39 }, 40 }, 41]); 42 43// Add some HTML code directly without grabbing (mostly for tests) 44c.add([ 45 { 46 html: "<title>This is a test</title>", 47 }, 48]);
please refer to options for detail.
Use rateLimit
to slow down when you are visiting web sites.
1import Crawler from "crawler";
2
3const c = new Crawler({
4 rateLimit: 1000, // `maxConnections` will be forced to 1
5 callback: (err, res, done) => {
6 console.log(res.$("title").text());
7 done();
8 },
9});
10
11c.add(tasks); //between two tasks, minimum time gap is 1000 (ms)
Sometimes you have to access variables from previous request/response session, what should you do is passing parameters in options.userParams :
1c.add({ 2 url: "http://www.google.com", 3 userParams: { 4 parameter1: "value1", 5 parameter2: "value2", 6 parameter3: "value3", 7 }, 8});
then access them in callback via res.options
1console.log(res.options.userParams);
If you are downloading files like image, pdf, word etc, you have to save the raw response body which means Crawler shouldn't convert it to string. To make it happen, you need to set encoding to null
1import Crawler from "crawler";
2import fs from "fs";
3
4const c = new Crawler({
5 encoding: null,
6 jQuery: false, // set false to suppress warning message.
7 callback: (err, res, done) => {
8 if (err) {
9 console.error(err.stack);
10 } else {
11 fs.createWriteStream(res.options.userParams.filename).write(res.body);
12 }
13 done();
14 },
15});
16
17c.add({
18 url: "https://raw.githubusercontent.com/bda-research/node-crawler/master/crawler_primary.png",
19 userParams: {
20 filename: "crawler.png",
21 },
22});
If you want to do something either synchronously or asynchronously before each request, you can try the code below. Note that direct requests won't trigger preRequest.
1import Crawler from "crawler"; 2 3const c = new Crawler({ 4 preRequest: (options, done) => { 5 // 'options' here is not the 'options' you pass to 'c.queue', instead, it's the options that is going to be passed to 'request' module 6 console.log(options); 7 // when done is called, the request will start 8 done(); 9 }, 10 callback: (err, res, done) => { 11 if (err) { 12 console.log(err); 13 } else { 14 console.log(res.statusCode); 15 } 16 }, 17}); 18 19c.add({ 20 url: "http://www.google.com", 21 // this will override the 'preRequest' defined in crawler 22 preRequest: (options, done) => { 23 setTimeout(() => { 24 console.log(options); 25 done(); 26 }, 1000); 27 }, 28});
Support both Promise and callback
1import Crawler from "crawler"; 2 3const crawler = new Crawler(); 4 5// When using directly "send", the preRequest won't be called and the "Event:request" won't be triggered 6const response = await crawler.send("https://github.com/"); 7console.log(response.options); 8// console.log(response.body); 9 10crawler.send({ 11 url: "https://github.com/", 12 // When calling `send`, `callback` must be defined explicitly, with two arguments `error` and `response` 13 callback: (error, response) => { 14 if (error) { 15 console.error(error); 16 } else { 17 console.log("Hello World!"); 18 } 19 }, 20});
Now we offer hassle-free support for using HTTP/2: just set http2
to true, and Crawler will operate as smoothly as with HTTP (including proxies).
Note: As most developers using this library with proxies also work with Charles, it is expected to set rejectAuthority
to false
in order to prevent the so-called 'self-signed certificate' errors."
1crawler.send({ 2 url: "https://nghttp2.org/httpbin/status/200", 3 method: "GET", 4 http2: true, 5 callback: (error, response) => { 6 if (error) { 7 console.error(error); 8 } 9 console.log(`inside callback`); 10 console.log(response.body); 11 }, 12});
Control the rate limit. All tasks submit to a rateLimiter will abide the rateLimit
and maxConnections
restrictions of the limiter. rateLimit
is the minimum time gap between two tasks. maxConnections
is the maximum number of tasks that can be running at the same time. rateLimiters are independent of each other. One common use case is setting different rateLimiters for different proxies. One thing is worth noticing, when rateLimit
is set to a non-zero value, maxConnections
will be forced to 1.
1import Crawler from "crawler"; 2 3const c = new Crawler({ 4 rateLimit: 2000, 5 maxConnections: 1, 6 callback: (error, res, done) => { 7 if (error) { 8 console.log(error); 9 } else { 10 const $ = res.$; 11 console.log($("title").text()); 12 } 13 done(); 14 }, 15}); 16 17// if you want to crawl some website with 2000ms gap between requests 18c.add("http://www.somewebsite.com/page/1"); 19c.add("http://www.somewebsite.com/page/2"); 20c.add("http://www.somewebsite.com/page/3"); 21 22// if you want to crawl some website using proxy with 2000ms gap between requests for each proxy 23c.add({ 24 url: "http://www.somewebsite.com/page/1", 25 rateLimiterId: 1, 26 proxy: "proxy_1", 27}); 28c.add({ 29 url: "http://www.somewebsite.com/page/2", 30 rateLimiterId: 2, 31 proxy: "proxy_2", 32}); 33c.add({ 34 url: "http://www.somewebsite.com/page/3", 35 rateLimiterId: 3, 36 proxy: "proxy_3", 37}); 38c.add({ 39 url: "http://www.somewebsite.com/page/4", 40 rateLimiterId: 4, 41 proxy: "proxy_1", 42});
Normally, all ratelimiters instances in the limiter cluster of crawler are instantiated with options specified in crawler constructor. You can change property of any rateLimiter by calling the code below. Currently, we only support changing property 'rateLimit' of it. Note that the default rateLimiter can be accessed by crawler.setLimiter(0, "rateLimit", 1000);
. We strongly recommend that you leave limiters unchanged after their instantiation unless you know clearly what you are doing.
1const crawler = new Crawler(); 2crawler.setLimiter(0, "rateLimit", 1000);
options
Emitted when a task is being added to scheduler.
1crawler.on("schedule", options => { 2 options.proxy = "http://proxy:port"; 3});
options
rateLimiterId
: number
Emitted when limiter has been changed.
options
Emitted when crawler is ready to send a request.
If you are going to modify options at last stage before requesting, just listen on it.
1crawler.on("request", options => { 2 options.searchParams.timestamp = new Date().getTime(); 3});
Emitted when queue is empty.
1crawler.on("drain", () => { 2 // For example, release a connection to database. 3 db.end(); // close connection to MySQL 4});
url | options
Add a task to queue and wait for it to be executed.
Number
Size of queue, read-only
You can pass these options to the Crawler() constructor if you want them to be global or as items in the crawler.add() calls if you want them to be specific to that item (overwriting global options)
silence
boolean
maxConnections
number
priorityLevels
number
rateLimit
Type: number
Default : 0
1000 means 1000 milliseconds delay between after the first request.
Note: This options is list as global only options because it will be set as the "default rateLimit value". This value is bound to a specific rate limiter and can only be modified through the crawler.setLimiter
method. Please avoid passing redundant rateLimit property in local requests; instead, use options.rateLimiterId
to specify a particular limiter.
Example:
1crawler.on("schedule", options => { 2 options.rateLimiterId = Math.floor(Math.random() * 15); 3});
skipDuplicates
boolean
homogeneous
boolean
userAgents
string | string[]
url | method | headers | body | searchParams...
forceUTF8
boolean
jQuery
boolean
encoding
string
rateLimiterId
number
retries
number
retryInterval
number
timeout
number
priority
number
skipEventRequest
boolean
html
boolean
proxies
string[]
1const ProxyManager = { 2 index: 0, 3 proxies: JSON.parse(fs.readFileSync("../proxies.json")), 4 setProxy: function (options) { 5 let proxy = this.proxies[this.index]; 6 this.index = ++this.index % this.proxies.length; 7 options.proxy = proxy; 8 options.rateLimiterId = Math.floor(Math.random() * 15); 9 }, 10}; 11 12crawler.on("schedule", options => { 13 // options.proxy = "http://127.0.0.1:8000"; 14 ProxyManager.setProxy(options); 15});
proxy
string
http2
boolean
referer
string
userParams
unknown
res.options
.preRequest
(options, done) => unknown
crawler.add
method.Callback
Type: (error, response, done) => unknown
Function that will be called after a request was completed
error
: Error catched by the crawlerresponse
: A response of standard IncomingMessage includes $
and options
response.options
: Options of this taskresponse.$
: jQuery Selector A selector for html or xml document.response.statusCode
: Number
HTTP status code. E.G.200
response.body
: Buffer
| String
| JSON
HTTP response content which could be a html page, plain text or xml document e.g.response.headers
: HTTP response headersdone
: The function must be called when you've done your work in callback. This is the only way to tell the crawler that the task is finished.Crawler by default use Cheerio. We are temporarily no longer supporting jsdom for certain reasons, may be later.
Options list here are renamed but most of the old ones are still supported for backward compatibility.
options.priorityRange
→ options.priorityLevels
options.uri
→ options.url
options.json
→ options.isJson
(Boolean. The "json" option is now work completely different.)
options.limiter
→ options.rateLimiterId
options.retryTimeout
→ options.retryInterval
crawler.direct
→ crawler.send
crawler.queue
→ crawler.add
crawler.setLimiterProperty
→ crawler.setLimiter
incomingEncoding
→ encoding
qs
→ searchParams
strictSSL
→ rejectUnauthorized
gzip
→ decompress
jar
→ cookieJar
(accepts tough-cookie
jar)
jsonReviver
→ parseJson
jsonReplacer
→ stringifyJson
Some practices that were acceptable and offen used in version 1 but not in version 2:
Crawler uses nock
to mock http request, thus testing no longer relying on http server.
1$ pnpm test
No vulnerabilities found.
Reason
all changesets reviewed
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
6 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 5
Reason
5 existing vulnerabilities detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-14
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More