spider-cheerio

cheerio

cheerio 为服务器特别定制的,快速,灵活, 实施的jQuery核心实现。
可用通过如下命令进行安装:
npm install cheerio

Features
相似的语法: 包括了jQuery核心的子集。
闪电般的快: 工作在一个非常简单,一致的DOM模型之上。高效的完成解析, 操作, 呈送。(基础的端到端的基准测试显示 Cheerio 大约比 JSDOM 快八倍)。
巨灵活: cheerio 封装了兼容 htmlparser。cheerio 几乎能够解析任何的 HTML 和 XML document。

简单的使用

  1. Loading

    1
    2
    var cheerio = require('cheerio');
    $ = cheerio.load('<ul id="fruites">...</ul>');

    或者通过传递字符串作为内容来加载 HTML:

    1
    2
    $ = require('cheerio');
    $('ul', '<ul id="fruits">...</ul>');

    Or as the root:

    1
    2
    $ = require('cheerio');
    $('li', 'ul', '<ul id="fruits">...</ul>');

    你也可以传递一个额外的对象给.load()如果你需要更改任何的默认解析选项的话:

    1
    2
    3
    4
    $ = cheerio.load('<ul id="fruits">...</ul>', {
    ignoreWhitespace: true,
    xmlMode: true
    });

    这些解析选项都是直接来自htmlparser ,因此任何在htmlparser里有效的选项在Chreeio里也是行得通的。默认的选项如下:

    1
    2
    3
    4
    5
       {
    ignoreWhitespace: false,
    xmlMode: false,
    lowerCaseTags: false
    }

参考链接: https://www.npmjs.com/package/cheerio