用node.js写的http小爬虫
来源:互联网 发布:淘宝选品数据分析 编辑:程序博客网 时间:2024/05/18 23:28
今天在慕课网学习到的一个小的http爬虫程序,还是蛮有意思的,分享一下。注意要安装node.js(自带 nmp),中间要用nmp引入cheerio。
/** * Created by lenovo on 2017-05-01. */const http = require('http');const url = 'http://www.imooc.com/learn/348';const cheerio = require('cheerio');function filterChapters(html){ var $ = cheerio.load(html); var chapters = $('.chapter'); var courseDate = []; chapters.each(function(item){ var chapter = $(this); var chapterTitle = chapter.find('strong').text(); var videos = chapter.find('.video').find('li'); var chapterDate = { chapterTitle : chapterTitle, videos : [] } videos.each(function(item){ var video = $(this).find('.J-media-item'); var videoTitle = video.text(); var videoId = video.attr('href').split('video/')[1]; chapterDate.videos.push({ title : videoTitle, id : videoId }) }) courseDate.push(chapterDate); }) return courseDate;}function printCourseDate(courseDate){ courseDate.forEach(function(item){ console.log(item.chapterTitle + '\n'); item.videos.forEach(function(video){ console.log('[' + video.id + ']' + video.title+'\n'); }) })}http.get(url,function(res){ var html=''; res.on('data',function(data){ html+=data; }) res.on('end',function(){ var courseDate = filterChapters(html); printCourseDate(courseDate); // console.log(courseDate); })}).on('error',function(){ console.log('出错了');});
喝喝,展示一下爬到的数据:
0 0
- 用node.js写的http小爬虫
- 用node.js实现http小爬虫
- node.js 小爬虫
- node js 小爬虫
- node.js爬虫小例子
- 使用node.js的http模块实现爬虫小工具
- node.js:利用cheerio编写的小爬虫程序
- Node.js小Http服务器
- node.js 小爬虫抓取网页数据
- Node.js爬虫模块小整合
- Node.js的简易爬虫
- 用node写的爬虫(一版)
- 用node写的爬虫(二版)
- 用node从零开始去写一个简单的爬虫
- node.js 教你写爬虫(附上gbk,gb2312中文乱码的解决方法)
- 慕课课程《进击Node.js基础(二)》的小爬虫
- PHP, Python, Node.js 哪个比较适合写爬虫?
- Node.js 的 http 模块
- PAT 1003 乙等 (我要通过) c++
- trunk函数详解
- 求数组中的次大值
- 数位DP-要49-基础DP-hdu3555-四种方法
- MyBatis架构设计及源代码分析(一):MyBatis架构
- 用node.js写的http小爬虫
- 1010: "水仙花数"问题1
- 菜鸟学习Spring——SpringMVC注解版控制层重定向到控制层
- PHP中include()与require()的区别
- Asp.Net Core 连接MySQL
- POJ 2313
- 计算几何 基础知识~具体的图像解决方案前导知识
- acm威佐夫博弈(裸题代码)
- 多线程下的单例模式