node爬虫(入门)

前段时间看到别人用node提取网页表格内容,今天自己尝试一下

依赖:node.js nginx

直接上代码吧!!!

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta http-equiv="Content-Style-Type" content="text/css">
  <title></title>
  <meta name="Generator" content="Cocoa HTML Writer">
  <meta name="CocoaVersion" content="2022.3">
  <style type="text/css">
  </style>
</head>
<body>
  <h3>2021全国各地男女占比</h3>
  <table border="1" cellspacing="0" cellpadding="0">
    <thead>
      <tr>
        <th>地区</th>
        <th></th>
        <th></th>
        <th>性别比</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>全国</td>
        <td>51.24</td>
        <td>48.76</td>
        <td>1105.07</td>
      </tr>
      <tr>
        <td>北京</td>
        <td>51.14</td>
        <td>48.86</td>
        <td>104.65</td>
      </tr>
      <tr>
        <td>天津</td>
        <td>51.53</td>
        <td>48.47</td>
        <td>106.31</td>
      </tr>
      <tr>
        <td>河北</td>
        <td>50.50</td>
        <td>49.50</td>
        <td>102.02</td>
      </tr>
      <tr>
        <td>江苏</td>
        <td>50.78</td>
        <td>49.22</td>
        <td>103.15</td>
      </tr>
    </tbody>
  </table>
</body>
</html>


在这里插入图片描述

const request = require('request');
const cheerio = require('cheerio');
const xlsx = require('node-xlsx');
const fs = require('fs');
const path = require('path');
const time = Date.now();

request('http://localhost:8080',(error,response,body)=>{
    if(!error && response && response.statusCode === 200) {
        const $ = cheerio.load(body);
        const th = $('thead th');
        const data = [];
        const arr = new Array(th.length).fill(0);
        const name = $('h3').text();
        const subtitle = arr.map((item,key)=> th.eq(key).text());
        $('tbody tr').each((key,item) => {
            const td = $(item).find('td');
            data.push(arr.map((item,key) => td.eq(key).text()));
        })
        data.unshift(subtitle);
        const buffer = xlsx.build([{name,data}]);
        fs.writeFileSync(path.join(__dirname,'gdp.xlsx'),buffer);
        console.log(`执行完成,用时${Math.ceil((Date.now() - time) / 1000)}`);
    }
})

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
第一次在mac电脑上安装nginx,看网上大家都推荐homebrew管理工具,安装的时候遇到了一些问题,查询一番总算解决了
命令步骤如下:
安装一:brew官网的安装脚本

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

安装二:brew 镜像安装脚本

/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"

安装nginx

brew install nginx

验证结果

brew services start nginx

启动成功后可在localhost:8080访问

安装问题参考文章如下:
mac安装homebrew问题
brew安装nginx报错‘Could not resolve HEAD to a revision’

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值