Cheerio 互动版

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

输出数据

现在把我们在 crawlerChapter 方法中得到的数据 data 组装,进行输出:

function printInfo(data) {
    data = data.filter(function filterByID(obj) {
        return obj.chapterTitle ? true : false;
    });
    data.map(function (item) {
        var chapterTitle = item.chapterTitle;
        console.log('【' + chapterTitle + '】\n');
        item.section.map(function (section) {
            console.log('   【' + section + '】\n');
        });
    })
};

方法解析:在 printInfo 方法中的参数 data ,这个参数需要 crawlerChapter 方法 return 给printInfo。然后就是 data 参数调用 filter 方法把数据为空的去掉。最后就是把章节拼接字符串进行输出。

在右侧代码框中第36行中把console.log(data) 替换成 return data,。

下面开始我们的任务:

  1. 在右侧代码框中第37行中把console.log(data) 替换成 return data
  2. 在右侧代码框中第12行中把crawlerChapter(html) 替换成 var chapter = crawlerChapter(html);
  3. 在右侧代码框中第13行中输入printInfo(chapter);
  4. 最后在点击提交运行按钮