node.js:利用cheerio编写的小爬虫程序
来源:互联网 发布:comic studio mac 编辑:程序博客网 时间:2024/05/17 21:57
之前在学习的node.js的时候跟随教学视频编写了一个利用cheerio实现的小爬虫程序,然而一直卡在cheerio的安装问题上运行无结果,今天终于给改出来了,写篇博客记录下自己犯蠢的过程,希望未来再接再厉。
(1)首先是node.js 和 git bash的安装,这个已经在上一篇博客里面说过了,这里不再重复;
(2)然后是cheerio插件的安装:
a) cheerio是nodejs的抓取页面模块,为服务器特别定制的,快速、灵活、实施的jQuery核心实现。适合各种Web爬虫程序。
b) cheerio的安装方法:
- git bash 当前操作目录为项目所在文件目录下;
- 执行安装指令之前需要先初始化,初始化的指令为: $ npm init -f
- 执行初始化指令之后会生成 package.json,检查package.json 文件中的内容是否缺少 repository、description等内容,否则的话等下安装还是会 warn;
- 执行指令:$ npm install cheerio ,安装cheerio插件;
- 运行指令:node--> require('cheerio') 查看返回结果用于检查cheerio是否安装正确;
c)图示:
(3)接下来放上小爬虫程序的源码,基本跟jQuery的语法无差别:
a) 简单源码:
var http = require('http');var url = "http://www.imooc.com/learn/348";http.get(url, function(res){ var html = ""; res.on('data', function(data){ html += data; }); res.on('end', function () { console.log(html); })}).on('error', function () { console.log('获取课程信息出错!')})
b) 升级版源码:
var http = require('http');var cheerio = require('cheerio');var url = "http://www.imooc.com/learn/348"; function filterChapters(html){ var $ = cheerio.load(html); var chapters = $('.chapter '); var courseData = []; chapters.each(function(item){ var chapter = $(this); var chapterTitle = chapter.find('strong').text(); var videos = chapter.find('.video').children('li'); var chapterData = { chapterTitle: chapterTitle.trim(), videos: [] } videos.each(function(item){ var video = $(this).find('.J-media-item'); var videoTitle = video.text(); var id = video.attr('href').split('video/')[1]; chapterData.videos.push({ id: id, title: videoTitle.trim() }); }) courseData.push(chapterData); }) return courseData; } function printCourseInfo(courseData) { courseData.forEach(function(item){ var chapterTitle = item.chapterTitle; console.log(chapterTitle+'\n'); item.videos.forEach(function(video){ console.log(' 【'+ video.id +'】 '+ video.title); }) }) }http.get(url, function(res){ var html = ""; res.on('data', function(data){ html += data; }); res.on('end', function () { var courseData = filterChapters(html); printCourseInfo(courseData); })}).on('error', function () { console.log('获取课程信息出错!')})
最后感叹一句,学知识真的太特么不容易了,找bug找的心都碎了( ̄へ ̄),以后还是要多读书多看报多跟友人打交道~
依旧附上我参考过的博客一篇:http://www.cnblogs.com/CraryPrimitiveMan/p/3674421.html
阅读全文
0 0
- node.js:利用cheerio编写的小爬虫程序
- node小爬虫(cheerio相当于jq)
- node.js使用cheerio制作网络爬虫
- node.js 爬虫乱码问题 cheerio
- node.js使用cheerio制作网络爬虫
- Node.js:request+cheerio爬虫爬取免费代理
- Node.js npm cheerio
- node.js 小爬虫
- node js 小爬虫
- Node.js中cheerio详解
- cheerio node爬虫开发小记录
- node.js 学习笔记003 :使用superagent和cheerio实现简单网页爬虫
- Node.js学习之网络爬虫(使用cheerio抓取网页数据)
- node.js爬虫小例子
- node 服务端的jquery---cheerio
- 使用node.js cheerio抓取网页数据
- 使用node.js cheerio抓取网页数据
- 使用node.js cheerio抓取网页数据
- SpringMVC 和Struts2的区别
- Spring--AOP-后置通知
- F1-F12的禁用方法
- Java实现AOP面向切面编程的实例教程
- oracle 用密码复杂度
- node.js:利用cheerio编写的小爬虫程序
- Kotlin资料
- Git使用
- 基于迅为4412精英版连接SIM7100C模块Android下调试——RIL库分析(二)
- 求二叉树的高度
- MySQL之事务&隔离级别&死锁
- Access 通过ODBC连接Oracle 数据库 (64位)
- 采样定理总结
- Java笔记——面向切面编程(AOP模式)