• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

bda-research/node-crawler: Web Crawler/Spider for NodeJS + server-side jQuery ;- ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

bda-research/node-crawler

开源软件地址:

https://github.com/bda-research/node-crawler

开源编程语言:

JavaScript 75.3%

开源软件介绍:

Node.js

npm package

CircleCI Coverage Status NPM download Package Quality Gitter

Most powerful, popular and production crawling/scraping package for Node, happy hacking :)

Features:

  • Server-side DOM & automatic jQuery insertion with Cheerio (default) or JSDOM,
  • Configurable pool size and retries,
  • Control rate limit,
  • Priority queue of requests,
  • forceUTF8 mode to let crawler deal for you with charset detection and conversion,
  • Compatible with 4.x or newer version.

Here is the CHANGELOG

Thanks to Authuir, we have a Chinese docs. Other languages are welcomed!

Table of Contents

Get started

Install

$ npm install crawler

Basic usage

const Crawler = require('crawler');

const c = new Crawler({
    maxConnections: 10,
    // This will be called for each crawled page
    callback: (error, res, done) => {
        if (error) {
            console.log(error);
        } else {
            const $ = 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: (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>'
}]);

Slow down

Use rateLimit to slow down when you are visiting web sites.

const Crawler = require('crawler');

const c = new Crawler({
    rateLimit: 1000, // `maxConnections` will be forced to 1
    callback: (err, res, done) => {
        console.log(res.$('title').text());
        done();
    }
});

c.queue(tasks);//between two tasks, minimum time gap is 1000 (ms)

Custom parameters

Sometimes you have to access variables from previous request/response session, what should you do is passing parameters as same as options:

c.queue({
    uri: 'http://www.google.com',
    parameter1: 'value1',
    parameter2: 'value2',
    parameter3: 'value3'
})

then access them in callback via res.options

console.log(res.options.parameter1);

Crawler picks options only needed by request, so don't worry about the redundancy.

Raw body

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

const Crawler = require('crawler');
const fs = require('fs');

const c = new Crawler({
    encoding: null,
    jQuery: false,// set false to suppress warning message.
    callback: (err, res, done) => {
        if (err) {
            console.error(err.stack);
        } else {
            fs.createWriteStream(res.options.filename).write(res.body);
        }

        done();
    }
});

c.queue({
    uri: 'https://nodejs.org/static/images/logos/nodejs-1920x1200.png',
    filename: 'nodejs-1920x1200.png'
});

preRequest

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.

const c = new Crawler({
    preRequest: (options, done) => {
        // '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
        console.log(options);
    	// when done is called, the request will start
    	done();
    },
    callback: (err, res, done) => {
        if (err) {
    	    console.log(err);
    	} else {
    	    console.log(res.statusCode);
    	}
    }
});

c.queue({
    uri: 'http://www.google.com',
    // this will override the 'preRequest' defined in crawler
    preRequest: (options, done) => {
        setTimeout(() => {
    	    console.log(options);
    	    done();
    	}, 1000);
    }
});

Advanced

Send request directly

In case you want to send a request directly without going through the scheduler in Crawler, try the code below. direct takes the same options as queue, please refer to options for detail. The difference is when calling direct, callback must be defined explicitly, with two arguments error and response, which are the same as that of callback of method queue.

crawler.direct({
    uri: 'http://www.google.com',
    skipEventRequest: false, // default to true, direct requests won't trigger Event:'request'
    callback: (error, response) => {
        if (error) {
            console.log(error)
        } else {
            console.log(response.statusCode);
        }
    }
});

Work with Http2

Node-crawler now supports http request. Proxy functionality for http2 request does not be included now. It will be added in the future.

crawler.queue({
    //unit test work with httpbin http2 server. It could be used for test
    uri: 'https://nghttp2.org/httpbin/status/200',
    method: 'GET',
    http2: true, //set http2 to be true will make a http2 request
    callback: (error, response, done) => {
        if (error) {
            console.error(error);
            return done();
        }

        console.log(`inside callback`);
        console.log(response.body);
        return done();
    }
})

Work with bottleneck

Control rate limit for with limiter. All tasks submit to a limiter 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. Limiters are independent of each other. One common use case is setting different limiters for different proxies. One thing is worth noticing, when rateLimit is set to a non-zero value, maxConnections will be forced to 1.

const crawler = require('crawler');

const c = new Crawler({
    rateLimit: 2000,
    maxConnections: 1,
    callback: (error, res, done) => {
        if (error) {
            console.log(error);
        } else {
            const $ = res.$;
            console.log($('title').text());
        }
        done();
    }
});

// if you want to crawl some website with 2000ms gap between requests
c.queue('http://www.somewebsite.com/page/1');
c.queue('http://www.somewebsite.com/page/2');
c.queue('http://www.somewebsite.com/page/3');

// if you want to crawl some website using proxy with 2000ms gap between requests for each proxy
c.queue({
    uri:'http://www.somewebsite.com/page/1',
    limiter:'proxy_1',
    proxy:'proxy_1'
});
c.queue({
    uri:'http://www.somewebsite.com/page/2',
    limiter:'proxy_2',
    proxy:'proxy_2'
});
c.queue({
    uri:'http://www.somewebsite.com/page/3',
    limiter:'proxy_3',
    proxy:'proxy_3'
});
c.queue({
    uri:'http://www.somewebsite.com/page/4',
    limiter:'proxy_1',
    proxy:'proxy_1'
});

Normally, all limiter instances in limiter cluster in crawler are instantiated with options specified in crawler constructor. You can change property of any limiter by calling the code below. Currently, we only support changing property 'rateLimit' of limiter. Note that the default limiter can be accessed by c.setLimiterProperty('default', 'rateLimit', 3000). We strongly recommend that you leave limiters unchanged after their instantiation unless you know clearly what you are doing.

const c = new Crawler({});
c.setLimiterProperty('limiterName', 'propertyName', value);

Class:Crawler

Event: 'schedule'

Emitted when a task is being added to scheduler.

crawler.on('schedule', (options) => {
    options.proxy = 'http://proxy:port';
});

Event: 'limiterChange'

Emitted when limiter has been changed.

Event: 'request'

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.

crawler.on('request', (options) => {
    options.qs.timestamp = new Date().getTime();
});

Event: 'drain'

Emitted when queue is empty.

crawler.on('drain', () => {
    // For example, release a connection to database.
    db.end();// close connection to MySQL
});

crawler.queue(uri|options)

Enqueue a task and wait for it to be executed.

crawler.queueSize

Size of queue, read-only

Options reference

You can pass these options to the Crawler() constructor if you want them to be global or as items in the queue() calls if you want them to be specific to that item (overwriting global options)

This options list is a strict superset of mikeal's request options and will be directly passed to the request() method.

Basic request options

Callbacks

  • callback(error, res, done): Function that will be called after a request was completed
    • error: Error
    • res: http.IncomingMessage A response of standard IncomingMessage includes $ and options
      • res.statusCode: Number HTTP status code. E.G.200
      • res.body: Buffer | String HTTP response content which could be a html page, plain text or xml document e.g.
      • res.headers: Object HTTP response headers
      • res.request: Request An instance of Mikeal's Request instead of http.ClientRequest
        • res.request.uri: urlObject HTTP request entity of parsed url
        • res.request.method: String HTTP request method. E.G. GET
        • res.request.headers: Object HTTP request headers
      • res.options: Options of this task
      • $: jQuery Selector A selector for html or xml document.
    • done: Function It must be called when you've done your work in callback.

Schedule options

  • options.maxConnections: Number Size of the worker pool (Default 10).
  • options.rateLimit: Number Number of milliseconds to delay between each requests (Default 0).
  • options.priorityRange: Number Range of acceptable priorities starting from 0 (Default 10).
  • options.priority: Number Priority of this request (Default 5). Low values have higher priority.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
mythz/jquip: jQuery in Parts发布时间:2022-07-07
下一篇:
petersommerhoff/jquery-course: Code from my Udemy course &quot;The Complete jQue ...发布时间:2022-07-07
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap