用 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('文件保存成功');
 })
 })
})
相关推荐
guoyunsky12 小时前
Ins爬虫可以抓取到国家,性别和年龄吗?
爬虫·数据分析·rpa
前端小趴菜~时倾13 小时前
自我提升-python爬虫学习:day03
爬虫·python·学习
Blurpath住宅代理13 小时前
HTTP与SOCKS5代理深度对比:从协议层到实战选型指南
爬虫·http·静态ip·动态代理·住宅ip·住宅代理
前端小趴菜~时倾15 小时前
自我提升-python爬虫学习:day04
爬虫·python·学习
feasibility.1 天前
AI 爬虫高手养成:Openclaw+Scrapling 手动部署 + 采集策略(以Walmart 电商平台为例)
人工智能·爬虫·科技·机器人·agi·openclaw·scrapling
vx_biyesheji00011 天前
Python 全国城市租房洞察系统 Django框架 Requests爬虫 可视化 房子 房源 大数据 大模型 计算机毕业设计源码(建议收藏)✅
爬虫·python·机器学习·django·flask·课程设计·旅游
胡耀超1 天前
Web Crawling 网络爬虫全景:技术体系、反爬对抗与全链路成本分析
前端·爬虫·python·网络爬虫·数据采集·逆向工程·反爬虫
itjinyin1 天前
初级爬虫实战——巴黎圣母院新闻
爬虫
vx_biyesheji00011 天前
计算机毕业设计:Python多源新闻数据智能舆情挖掘平台 Flask框架 爬虫 SnowNLP ARIMA 可视化 数据分析 大数据(建议收藏)✅
爬虫·python·机器学习·数据分析·django·flask·课程设计
j_xxx404_1 天前
爬虫对抗:ZLibrary反爬机制实战分析 (三) - 突破高频访问限制与TLS指纹(JA3)风控
爬虫