node小爬虫

本身就想玩一下爬虫这玩意,看到http://www.imooc.com/video/7965,诺,关于node的爬虫视频,就省的自己研究了,当然,视频中爬的html和现在的有不同,并不是很影响。

为了复习一下fs模块,就爬了之后写成自己想要的形式。

将文件爬出来,并把需要的部分存成自己想要的格式,然后写入一个txt文件,当然,后期通过这也可以写入数据库,想怎么玩就怎么玩了。以下是爬出来的文件。

小伙伴肯定迫不及待看源代码了吧。

直接贴源代码了:

--------------------------------------------------------------------我是分割线

var http = require(‘http‘);
var fs = require(‘fs‘);
var util = require(‘util‘);
var cheerio = require(‘cheerio‘);

var imooc_url = ‘http://www.imooc.com/learn/348‘;

function filterChapters(html) {
fs.stat(‘crawler.html‘, function(err, stats) {
if (err) {
fs.writeFile(‘crawler.html‘, html, function(err) {
if (err) return console.log(err);
console.log(‘写入成功!‘);
});
} else {
console.log(‘文件已存在‘);
}
});
var $ = cheerio.load(html);
var chapters = $(‘.chapter‘);
// [{
// chapterTitle:‘‘,
// videos:[{
// title:‘‘,
// id:‘‘
// },{
// title:‘‘,
// id:‘‘
// }]
// }]
var courseData = [];
chapters.each(function(item) {
var chapter = $(this);
var chapterTitle = chapter.find(‘strong‘).text().replace(/\s+/g,‘ ‘);
var videos = chapter.find(‘.video‘).children(‘li‘);
var chapterData = {
chapterTitle: chapterTitle,
videos: []
}
videos.each(function(item) {
var video = $(this).find(‘.J-media-item‘);
var videoTitle = video.text().replace(/\s+/g,‘ ‘).replace(‘开始学习‘,‘‘);
console.log(videoTitle);
var id = video.attr(‘href‘).split(‘video/‘)[1].replace(/\s+/g,‘ ‘);
chapterData.videos.push({
title: videoTitle,
id: id
});
});
courseData.push(chapterData);
});
return courseData;
}

function printCourseInfo(courseData) {
//console.log(util.inspect(courseData));
courseData.forEach(function(item) {
var chapterTitle = item.chapterTitle;
fs.appendFileSync(‘crawler.txt‘, chapterTitle);
fs.appendFileSync(‘crawler.txt‘, ‘\n‘);
item.videos.forEach(function(video) {
var v = ‘ \t 【 ‘ + video.id + ‘ 】 ‘ + video.title;
fs.appendFileSync(‘crawler.txt‘, v);
fs.appendFileSync(‘crawler.txt‘, ‘\n‘);
})

});

}

http.get(imooc_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(‘获取出错‘);
})

--------------------------------------------------------------------------我是分割线

http.get(imooc_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(‘获取出错‘);
})

显示请求了慕课的一个网址,然后把数据保存为变量,传给章节过滤函数,然后打印信息。

过滤函数中,为了方便我查看,先是把它存为html文件。

fs.stat(‘crawler.html‘, function(err, stats) {
if (err) {
fs.writeFile(‘crawler.html‘, html, function(err) {
if (err) return console.log(err);
console.log(‘写入成功!‘);
});
} else {
console.log(‘文件已存在‘);
}
});

加载cheerio模块,构建自己需要的数据,由于跟jquery操作差不多,就不需要多解释了。

打印的时候将其以同步追加的方式写入txt文件中,

courseData.forEach(function(item) {
var chapterTitle = item.chapterTitle;
fs.appendFileSync(‘crawler.txt‘, chapterTitle);
fs.appendFileSync(‘crawler.txt‘, ‘\n‘);
item.videos.forEach(function(video) {
var v = ‘ \t 【 ‘ + video.id + ‘ 】 ‘ + video.title;
fs.appendFileSync(‘crawler.txt‘, v);
fs.appendFileSync(‘crawler.txt‘, ‘\n‘);
})

});

//如果慕课网这个页面又变了就得改改构造数据的那一块了。

现在的chapter结构是这样的

<div class="chapter ">
<!-- 章节标题 -->
<h3>

<span class="icon-drop_down js-close"></span>
<strong>
<i class="icon-chapter"></i>
第1章 前言
<div class="icon-info chapter-info">
<i class="icon-drop_up triangle">
<div class="chapter-introubox">
<div class="chapter-content">带你了解为什么要学习 Nodejs。</div>
</div>
</i>
</div>
</strong>

</h3>
<!-- 章节标题 end -->
<!-- 章节小节 -->
<ul class="video">
<li data-media-id="6687">
<a href=‘/video/6687‘ class="J-media-item">
<i class="icon-video type"></i>
1-1 Node.js基础-前言
(01:20)

<button class="r moco-btn moco-btn-red preview-btn">开始学习</button>

</a>
<!-- 未登录时 -->
<!-- <a target="_blank" href="/video/1430" class="J-media-item studyvideo">1-1 Java简介 (05:49)
<button class="r moco-btn moco-btn-blue preview-btn">预览</button>
</a> -->
</li>
<li data-media-id="6688">
<a href=‘/video/6688‘ class="J-media-item">
<i class="icon-video type"></i>
1-2 为什么学习Nodejs
(05:43)

<button class="r moco-btn moco-btn-red preview-btn">开始学习</button>

</a>
<!-- 未登录时 -->
<!-- <a target="_blank" href="/video/1430" class="J-media-item studyvideo">1-1 Java简介 (05:49)
<button class="r moco-btn moco-btn-blue preview-btn">预览</button>
</a> -->
</li>
</ul>
<!-- 章节小节 end -->
</div>

时间: 2024-08-05 07:06:16

node小爬虫的相关文章

Node.js(四)【HTTP小爬虫】

HTTP源码解读 HTTP性能测试 1 var http = require('http'); 2 3 http 4 .createServer(function (request, response) { 5 response.writeHead(200, {'Content-type': 'text/plain'}); 6 response.write('Hello Nodejs'); 7 response.end(); 8 }) 9 .listen(2016); 10 11 console

Node.js 爬虫批量下载美剧 from 人人影视 HR-HDTV

这两天发现了一个叫看知乎的网站,是知乎大牛苏莉安做的,其中爬虫使用的 Node.js.这里就针对上一篇博客中的美剧小爬虫,改用 nodejs 进行实现一下,体验一下强大的 Node.js. 如果之前没有用过 JavaScript,不妨到 http://www.codecademy.com/  做一下 JavaScript 和 jQuery 的入门练习,快速熟悉一下基本语法,有其他语言基础一天时间足够.有基本的了解后,就会发现 JavaScript 的两大特点: 使用基于原型(prototype)

http 小爬虫

初学nodejs写一个http小爬虫,爬虫就是把网页上的代码爬下来. 代码: var http = require('http') //加载http模块var url = 'http://www.imooc.com/learn/713' http.get(url,function(res){   //get去请求url,此处以慕课网为例 var html = '' res.on('data',function(data){ html += data     //请求数据赋值给前面定义的html

nodejs .http模块, cheerio模块 实现 小爬虫.

代码: 1 var http = require("http"); 2 3 var cheerio = require("cheerio"); 4 5 6 var url = 'http://www.imooc.com/learn/348'; 7 8 9 http.get(url, function(res){ 10 var html = ''; 11 12 res.on('data', function(data){ 13 html += data; 14 });

Java豆瓣电影爬虫——小爬虫成长记(附源码)

以前也用过爬虫,比如使用nutch爬取指定种子,基于爬到的数据做搜索,还大致看过一些源码.当然,nutch对于爬虫考虑的是十分全面和细致的.每当看到屏幕上唰唰过去的爬取到的网页信息以及处理信息的时候,总感觉这很黑科技.正好这次借助梳理Spring MVC的机会,想自己弄个小爬虫,简单没关系,有些小bug也无所谓,我需要的只是一个能针对某个种子网站能爬取我想要的信息就可以了.有Exception就去解决,可能是一些API使用不当,也可能是遇到了http请求状态异常,又或是数据库读写有问题,就是在这

HTTP小爬虫 ,nodejs学习(二)

使用nodejs抓取网页数据,这里用到cheerio,解析html十分好用,和jquery用法完全一致. 首先安装cheerio,在命令行中输入 npm install cheerio;(在nodejs根目录下输入该命令) 安装完成以后,我们来解析慕课网上http://www.imooc.com/learn/348,获取其上的课程信息. 代码如下: var http = require('http'); var cheerio = require('cheerio'); var url = 'h

用NodeJs做一个小爬虫

作者:北京起步科技前端研究员,专注分享HTML5 App快速开发工具 WeX5 的黑魔法以及相应的前端技术. 前言 利用爬虫可以做很多事情,单身汉子们可以用爬虫来收集各种妹子情报,撩妹族们可以用爬虫收集妹子想要的小东西,赚大钱的人可以用来分析微博言论与股票涨跌的关系诸如此类的,简直要上天了. 你们感受一下 点我点我: 蠢蠢欲动 抛开机器学习这种貌似很高大上的数据处理技术,单纯的做一个爬虫获取数据还是非常简单的.对于前段er们来说,生在有nodejs的年代真是不要太幸福了,下面就用nodejs来做

今天来做一个PHP电影小爬虫。

今天来做一个PHP电影小爬虫.我们来利用simple_html_dom的采集数据实例,这是一个PHP的库,上手很容易.simple_html_dom 可以很好的帮助我们利用php解析html文档.通过这个php封装类可以很方便的解析html文档,对其中的html元素进行操作 (PHP5+以上版本)下载地址:https://github.com/samacs/simple_html_dom下面我们以 http://www.paopaotv.com 上的列表页 http://paopaotv.com

python之小爬虫

#!/usr/bin/python #抓取网页上的图片保存 import urllib import urllib.request //python3版本将urllib2分成urllib.request和urllib.error import re def gethtml(url): page = urllib.request.urlopen(url) html = page.read() return html def getImages(html): reg = r'src="(.*?\.j