直接上代码吧!!!
<!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’