你不知道的 Node.js 爬虫原来这么简单
今天给大家带来的是node简单爬虫,对于前端小白也是非常好理解且会非常有成就感的小技能
爬虫的思路可以总结为:请求 url - > html(信息) -> 解析html
这篇文章呢,就带大家爬取豆瓣TOP250电影的信息
工具
爬虫必备工具:cheerio
cheerio
简单介绍:cheerio
是 jquery
核心功能的一个快速灵活而又简洁的实现,主要是为了用在服务器端需要对 DOM
进行操作的地方。大家可以简单的理解为用来解析 html
非常方便的工具。使用之前只需要在终端安装即可 npm install cheerio
node爬虫步骤解析
一、选取网页url,使用http协议get到网页数据
豆瓣TOP250链接地址:https://movie.douban.com/top250
首先我们请求http协议,通过http来拿到网页的所有数据
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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); }) }) |
上面代码呢,大家一定要注意我们请求数据时,拿到的数据是分段拿到的,我们需要通过自己把数据拼接起来
1 2 3 | res.on( 'data' , function (chunk){ html += chunk; }) |
拼接完成时 我们可以输出一下,看一下我们是否拿到了完整数据
1 2 3 | res.on( 'end' , function (){ console.log(html); }) |
二、使用cheerio工具解析需要的内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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 }) }) |
可以通过检查网页源代码查看需要的内容在哪个标签下面,然后通过$符号来拿到需要的内容,这里我就拿了电影的名字、评分、电影图片
到了这时候,你会发现,node
爬虫实现是非常简单的,我们只需要认真分析一下我们拿到的 html
数据,将需要的内容拿出来保存在本地就基本完成了
保存数据
下面就是保存数据了,我将数据保存在 films.json
文件中 将数据保存到文件中,我们引入一个fs模块,将数据写入文件中去
1 2 3 4 5 6 | const fs = require( 'fs' ); fs.writeFile( './films.json' , JSON.stringify(allFilms), function (err){ if (!err){ console.log( '文件写入完毕' ); } }) |
文件写入代码需要写在 res.on('end')
里面,数据读完->写入 写入完成,可以查看一下 films.json
,里面是有爬取的数据的。
下载图片
我们爬取的图片数据是图片地址,如果我们要将图片保存到本地呢?这时候只需要跟前面请求网页数据一样,把图片地址url请求回来,每一张图片写入到本地即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function downloadImage(allFilms) { for ( let i = 0; i < allFilms.length; 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')
里面调用一下函数就大功告成了
源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | // 请求 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<allFilms.length; 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
学习有很大的帮助,本文的爬虫技巧只是入门,感兴趣小伙伴可以继续探究。
转自:微信公众号:
https://mp.weixin.qq.com/s/HYISusLzIqdBWaBjojrpjg
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2019-03-28 关于在scrapy中使用xpath
2019-03-28 scrapy爬取虎嗅(典型的post请求在scrapy中的应用)