首页 > 编程语言 >【node爬虫】node爬虫实用教程

【node爬虫】node爬虫实用教程

时间:2023-10-09 11:23:47浏览次数:30  
标签:node function 实用教程 const cheerio 爬虫 html https

准备工作

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

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

完整代码

//引入模块
const https = require('https')
const cheerio = require('cheerio')
const fs = require('fs')
//获取页面的html结构
// https.get('https://movie.douban.com/top250', function (res) {
    https.get('https://book.douban.com/chart?subcat=all', function (res) {
    let html = ''
    res.on('data', function (chunk) {
        //console.log(chunk + '');
        html += chunk
    })
    res.on('end', function () {
        // 获取html中的数据
        const $ = cheerio.load(html)
        console.log(html,'html')
        let allFiles = []
        //拿到每一个item中我们需要的数据
        $('.media').each(function () {
         
            const title = $('.media__body h2 .fleft', this).text()
            const star = $('.media__body p .ml8', this).text()
            const pic = $('.media__img .subject-cover', this).attr('src')
            const price = $('.buy-info a', this).text().trim()
            //数据以对象的形式存放在数组中
            allFiles.push({
                title: title,
                star: star,
                pic: pic,
                price
            })
        })
        //console.log(allFiles);
        //将数据写入文件中
        fs.writeFile('./files.json', JSON.stringify(allFiles), function (err, data) {
            if (err) {
                throw err
            }
            console.log('文件保存成功');
        })
    })
})

目标列表

 

成果截图

 

标签:node,function,实用教程,const,cheerio,爬虫,html,https
From: https://www.cnblogs.com/xiaohuizhang/p/17751040.html

相关文章

  • 基于Node.js的大文件分片上传
    我们在做文件上传的时候,如果文件过大,可能会导致请求超时的情况。所以,在遇到需要对大文件进行上传的时候,就需要对文件进行分片上传的操作。同时如果文件过大,在网络不佳的情况下,如何做到断点续传?也是需要记录当前上传文件,然后在下一次进行上传请求的时候去做判断。前端1.index.ht......
  • node-oracledb nodejs 包支持thin 模式了
    node-oracledb从v6.0.0版本来时支持thin模式了,是基于纯javascript开发的驱动,我们终于可以不用依赖比较笨重的oracleclient了属于一个很大的进度,同时python版本也是支持thin模式了,我以前也简单介绍过参考资料https://github.com/oracle/node-oracledb/releaseshttps://me......
  • 花瓣网爬虫
    fromseleniumimportwebdriverfromselenium.webdriver.common.byimportByimporttimeimportosBASE_URL="https://huaban.com/search?q={keyword}&sort=all&type=board&category=industrial_design"defsearch_and_save_links(driver,keyword,......
  • 异步爬虫实战:实际应用asyncio和aiohttp库构建异步爬虫
    在网络爬虫的开发中,异步爬虫已经成为一种非常流行的技术。它能够充分利用计算机的资源,提高爬虫效率,并且能够处理大量的运算请求。Python中的asyncio和aiohttp库提供了强大的异步爬虫支持,使得开发者能够轻松构建高效的异步爬虫。什么是异动爬虫?为什么要使用自动爬虫?异步爬虫是一......
  • redis-cluster nodes命令信息说明
     集群定义1.1每个字段的含义如下:1.id:节点ID,一个40字节的随机字符串,节点创建时生成,且不会变化(除非使用CLUSTERRESETHARD命令)。2.ip:port:客户端访问的地址。3.flags:逗号分隔的标记位,可能值有:myself,master,slave,fail?,fail,handshake,noaddr,noflags......
  • node版本管理和安装
    1.使用nvm进行node版本管理。在window下安装nvm,下载地址:https://github.com/coreybutler/nvm-windows,下载exe包进行安装。2.安装完成后打开cmd,输入nvm-v查看版本。显示:1.1.11具体版本根据你下载的版本。(如果在安装之前已经安装过node,会有询问框是否把已经存在的版本纳入管理,......
  • nodemailer实现发送邮件后跳转静态页面
    写了一个静态展示页面,并通过node.js中的nodemailer实现发送邮件服务,发送结束后跳转到其他静态页面。点击查看代码constexpress=require('express');constnodemailer=require('nodemailer');constapp=express();//解析url-encoded格式的表单数据app.use(express......
  • ansible-playbook批量安装httpd,按主机名提供不同的index.html(如node1的index.html欢迎
    [root@ansible~]#vim/etc/ansible/hosts[webservers]10.0.0.150ansible_connection=local10.0.0.160#创建角色相关目录[root@ansiblehtml]#mkdir-pv/data/ansible/roles/httpd/{tasks,handlers,files}mkdir:createddirectory'/data/ansible'mkdir:crea......
  • 一个电脑管理多个node版本 (不同项目使用不同版本的node)
    一、介绍使用nvm可以方便的在同一台设备上进行多个node版本之间切换,解决不同的项目所使用的node版本不一样的问题二、安装nvm1、NVM下载(1)如果已经单独安装了node,请先卸载再继续(2)进入安装包下载地址:https://github.com/coreybutler/nvm-windows/releases,选择nvm-setup.zip ......
  • Node.js Assertion API All In One
    Node.jsAssertionAPIAllInOneNode.js断言API严格模式Instrictassertionmode,non-strictmethodsbehaveliketheircorrespondingstrictmethods.Forexample,assert.deepEqual()willbehavelikeassert.deepStrictEqual().在严格断言模式下,非严格方法的行......