node 爬虫mysql_你不知道的node爬虫原来这么简单

前言

今天给大家带来的是node简单爬虫,对于前端小白也是非常好理解且会非常有成就感的小技能

爬虫的思路可以总结为:请求 url - > html(信息) -> 解析html

这篇文章呢,就带大家爬取豆瓣TOP250电影的信息

工具

爬虫必备工具:cheerio

cheerio简单介绍:cheerio是jquery核心功能的一个快速灵活而又简洁的实现,主要是为了用在服务器端需要对DOM进行操作的地方。 大家可以简单的理解为用来解析html非常方便的工具。

使用之前只需要在终端安装即可 npm install cheerio

node爬虫步骤解析

一、选取网页url,使用http协议get到网页数据

豆瓣TOP250链接地址:https://movie.douban.com/top250

8deb79f42c7996aba39d1da027feb278.png

首先我们请求http协议,通过http来拿到网页的所有数据

const https = require('https');

https.get('https://movie.douban.com/top250',function(res){

// 分段返回的 自己拼接

let html = '';

// 有数据产生的时候 拼接

res.on('data',function(chunk){

html += chunk;

})

// 拼接完成

res.on('end',function(){

console.log(html);

})

})

复制代码

上面代码呢,大家一定要注意我们请求数据时,拿到的数据是分段拿到的,我们需要通过自己把数据拼接起来

res.on('data',function(chunk){

html += chunk;

})

复制代码

拼接完成时 我们可以输出一下,看一下我们是否拿到了完整数据

res.on('end',function(){

console.log(html);

})

复制代码

二、使用cheerio工具解析需要的内容

const cheerio = require('cheerio');

res.on('end',function(){

console.log(html);

const $ = cheerio.load(html);

let allFilms = [];

$('li .item').each(function(){

// this 循环时 指向当前这个电影

// 当前这个电影下面的title

// 相当于this.querySelector

const title = $('.title', this).text();

const star = $('.rating_num',this).text();

const pic = $('.pic img',this).attr('src');

// console.log(title,star,pic);

// 存 数据库

// 没有数据库存成一个json文件 fs

allFilms.push({

title,star,pic

})

})

复制代码

可以通过检查网页源代码查看需要的内容在哪个标签下面,然后通过$符号来拿到需要的内容,这里我就拿了电影的名字、评分、电影图片

889a23e05bdcd1fa6486a99e790c12d6.png

37e2641926458ec857bd0e0ec6f9cabf.png

082a4027bed29485130bc1e41151d855.png

到了这时候,你会发现,node爬虫实现是非常简单的,我们只需要认真分析一下我们拿到的html数据,将需要的内容拿出来保存在本地就基本完成了

保存数据

下面就是保存数据了,我将数据保存在films.json文件中

将数据保存到文件中,我们引入一个fs模块,将数据写入文件中去

const fs = require('fs');

fs.writeFile('./films.json', JSON.stringify(allFilms),function(err){

if(!err){

console.log('文件写入完毕');

}

})

复制代码

文件写入代码需要写在res.on('end')里面,数据读完->写入

写入完成,可以查看一下films.json,里面是有爬取的数据的。

下载图片

我们爬取的图片数据是图片地址,如果我们要将图片保存到本地呢? 这时候只需要跟前面请求网页数据一样,把图片地址url请求回来,每一张图片写入到本地即可

function downloadImage(allFilms){

for(let i=0; i

const picUrl = allFilms[i].pic;

// 请求 -> 拿到内容

// fs.writeFile('./xx.png','内容')

https.get(picUrl,function(res){

res.setEncoding('binary');

let str = '';

res.on('data',function(chunk){

str += chunk;

})

res.on('end',function(){

fs.writeFile(`./images/${i}.png`,str,'binary',function(err){

if(!err){

console.log(`第${i}张图片下载成功`);

}

})

})

})

}

}

复制代码

下载图片的步骤跟爬取网页数据的步骤是一模一样的,我们将图片的格式保存为.png

写好了下载图片的函数,我们在res.on('end')里面调用一下函数就大功告成了

源码

// 请求 url - > html(信息) -> 解析html

const https = require('https');

const cheerio = require('cheerio');

const fs = require('fs');

// 请求 top250

// 浏览器输入一个 url, get

https.get('https://movie.douban.com/top250',function(res){

// console.log(res);

// 分段返回的 自己拼接

let html = '';

// 有数据产生的时候 拼接

res.on('data',function(chunk){

html += chunk;

})

// 拼接完成

res.on('end',function(){

console.log(html);

const $ = cheerio.load(html);

let allFilms = [];

$('li .item').each(function(){

// this 循环时 指向当前这个电影

// 当前这个电影下面的title

// 相当于this.querySelector

const title = $('.title', this).text();

const star = $('.rating_num',this).text();

const pic = $('.pic img',this).attr('src');

// console.log(title,star,pic);

// 存 数据库

// 没有数据库存成一个json文件 fs

allFilms.push({

title,star,pic

})

})

// 把数组写入json里面

fs.writeFile('./films.json', JSON.stringify(allFilms),function(err){

if(!err){

console.log('文件写入完毕');

}

})

// 图片下载一下

downloadImage(allFilms);

})

})

function downloadImage(allFilms){

for(let i=0; i

const picUrl = allFilms[i].pic;

// 请求 -> 拿到内容

// fs.writeFile('./xx.png','内容')

https.get(picUrl,function(res){

res.setEncoding('binary');

let str = '';

res.on('data',function(chunk){

str += chunk;

})

res.on('end',function(){

fs.writeFile(`./images/${i}.png`,str,'binary',function(err){

if(!err){

console.log(`第${i}张图片下载成功`);

}

})

})

})

}

}

复制代码

总结

爬虫不是只有python才行的,我们node也很方便简单,前端新手掌握一个小技能也是非常不错的,对自身的node学习有很大的帮助,欢迎大家留言讨论

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值