用 Node.js 写一个爬虫

自己设计一个网站,然后去爬取别人家页面的数据来做一个自己的网站。哈哈哈,如果自己写着玩可能没啥事,但如果用这个网站来获利,你可能就要被寄律师函了,毕竟这有点'刑'。这篇文章呢,就带大家爬取豆瓣TOP250电影的信息。豆瓣电影 Top 250 \(douban.com\)[1]

准备工作

  1. 通过指令npm init初始化文件夹,会获得package.json项目说明书。

  2. 爬虫必备工具:cheerio;通过在终端输入npm i cheerio,即可将文件装到项目里。cheeriojquery 核心功能的一个快速灵活而又简洁的实现,主要是为了用在服务器端需要对 DOM 进行操作的地方。大家可以简单的理解为用来解析 html 非常方便的工具。

开始(细分七步)

  1. 用https模块(node直接提供给我们的)获取网站地址,通过get方法读取网站地址上的数据。

    const https = require('https')
    https.get('https://movie.douban.com/top250', function (res) {
    let html = ''
    res.on('data', function (chunk) {
    //console.log(chunk + '');
    //得到数据流,通过字符串拼接得到html结构
    html += chunk
    })

这样会读取到整个页面的html结构。

  1. 通过 res.on('end', function () {}),保证读取完了才会去做操作。

  2. 引入cheerio

const cheerio = require('cheerio')

  1. 获取html中的数据

    const = cheerio.load(html) ('li .item').each(function () {
    const title = ('.title', this).text() const star = ('.info .bd .rating_num', this).text()
    const pic = $('.pic img', this).attr('src')
    })

这里需要注意的是我们可以去页面上看我们需要拿到哪个类名里面的内容,通过$符号可以拿到内容。

  1. 创建一个空数组,把数据以对象的形式存放在数组中

    let allFiles = []
    allFiles.push({
    title: title,
    star: star,
    pic: pic
    })

我们可以通过console.log(allFiles)来检查是否打印出来了我们需要的结果。

  1. 将数据写入文件,引用node官方提供的模块fs

const fs = require('fs')

  1. 创建文件夹files.json,向其中写入数据

    fs.writeFile('./files.json', JSON.stringify(allFiles), function (err, data) {
    if (err) {
    throw err
    }
    console.log('文件保存成功');
    })

到这之后,我们可以看到在当前文件夹下自动创建了文件files.json,里面已经有了我们想要的数据。

完整代码

复制代码
//引入模块
const https = require('https')
const cheerio = require('cheerio')
const fs = require('fs')
//获取页面的html结构
https.get('https://movie.douban.com/top250', function (res) {
 let html = ''
 res.on('data', function (chunk) {
 //console.log(chunk + '');
 html += chunk
 })
 res.on('end', function () {
 // 获取html中的数据
 const $ = cheerio.load(html)
 let allFiles = []
 //拿到每一个item中我们需要的数据
 $('li .item').each(function () {
 const title = $('.title', this).text()
 const star = $('.info .bd .rating_num', this).text()
 const pic = $('.pic img', this).attr('src')
 //数据以对象的形式存放在数组中
 allFiles.push({
 title: title,
 star: star,
 pic: pic
 })
 })
 //console.log(allFiles);
 //将数据写入文件中
 fs.writeFile('./files.json', JSON.stringify(allFiles), function (err, data) {
 if (err) {
 throw err
 }
 console.log('文件保存成功');
 })
 })
})
相关推荐
小白学大数据8 小时前
基于Python的新闻爬虫:实时追踪行业动态
开发语言·爬虫·python
小白iP代理10 小时前
动态IP+AI反侦测:新一代爬虫如何绕过生物行为验证?
人工智能·爬虫·tcp/ip
叫我:松哥1 天前
基于网络爬虫的在线医疗咨询数据爬取与医疗服务分析系统,技术采用django+朴素贝叶斯算法+boostrap+echart可视化
人工智能·爬虫·python·算法·django·数据可视化·朴素贝叶斯
bksheng1 天前
【SSL证书校验问题】通过 monkey-patch 关掉 SSL 证书校验
网络·爬虫·python·网络协议·ssl
叫我:松哥1 天前
优秀案例:基于python django的智能家居销售数据采集和分析系统设计与实现,使用混合推荐算法和LSTM算法情感分析
爬虫·python·算法·django·lstm·智能家居·推荐算法
xnglan2 天前
使用爬虫获取游戏的iframe地址
开发语言·爬虫·python·学习
荼蘼2 天前
python爬虫实战-小案例:爬取苏宁易购的好评
开发语言·爬虫·python
香蕉可乐荷包蛋2 天前
爬虫基础概念
爬虫
小白学大数据3 天前
多线程Python爬虫:加速大规模学术文献采集
开发语言·爬虫·python·自动化
不老刘3 天前
Charles 的 Windows proxy 对爬取瑞数6 网站接口数据的作用分析
爬虫·python·drissionpage·瑞数