-
Notifications
You must be signed in to change notification settings - Fork 3
/
crawler.js
48 lines (42 loc) · 1.08 KB
/
crawler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var Crawler = require("crawler");
var c = new Crawler({
maxConnections: 10,
// This will be called for each crawled page
callback: function (error, res, done) {
if (error) {
console.log(error);
} else {
var $ = res.$;
// $ is Cheerio by default
//a lean implementation of core jQuery designed specifically for the server
console.log($("title").text());
}
done();
},
});
// Queue just one URL, with default callback
c.queue("http://www.amazon.com");
// Queue a list of URLs
c.queue(["http://www.google.com/", "http://www.yahoo.com"]);
// Queue URLs with custom callbacks & parameters
c.queue([
{
uri: "http://parishackers.org/",
jQuery: false,
// The global callback won't be called
callback: function (error, res, done) {
if (error) {
console.log(error);
} else {
console.log("Grabbed", res.body.length, "bytes");
}
done();
},
},
]);
// Queue some HTML code directly without grabbing (mostly for tests)
c.queue([
{
html: "<p>This is a <strong>test</strong></p>",
},
]);