Nodejs 批量检测 Excel 中url链接是否可访问
最近领导给我一个Excel表格,让我检查下其中的 Url 是否有效。作为一个开发人员,我内心是拒绝的,一个一个点击测试,估计手抽筋也测试不完。我想,人类的进步,源于使用工具,那我就写个程序跑一下吧,然后我去喝杯茶。
思路
- 读取 Excel 中数据
- 检测 url 是否有效
- 把数据写入 Excel
- 检查核对下数据
Excel 格式示例
序号 | 分类 | 名称 | 网址 | 状态 |
---|---|---|---|---|
1 | A类 | 某某网 | http://www.xxx.com/ | 未检测 |
2 | A类 | 某某网 | http://www.xxx.com/ | 未检测 |
3 | A类 | 某某网 | http://www.xxx.com/xxx | 未检测 |
撸起袖子,动手干吧
根据思路, 整理下代码吧
新建 index.js
const urlIsSuccess = require('./urlIsSuccess')
const { readSheets, getSheet, sheetsBuffer, writeFile } = require('./xlsx')const entryfilePath = `${ __dirname }/test.xlsx` // 输入的 Excel 文件
const outFilePath = `${ __dirname }/out_test.xlsx` // 输出的 Excel 文件
const sheetIndex = 0; // Excel 的 Sheet 索引
const urlColIndex = 3 // Excel 中 网址 所在的列索引
const statusColIndex = 4 // Excel 中 状态 所在的列索引let sheets = readSheets(entryfilePath) // 读取 Excel 中数据
const sheet = getSheet(sheets, sheetIndex) // 读取 Excel 中 Sheet 数据const isUrl = url => {if (!url) return falsereturn /\s*http(s)?:\/\/.+/.test(url)
}// 循环sheet表中的行数据
// 返回一个Promise数组, 用于 Promise.all 使用
let promises = sheet.map(rows => {const url = rows[ urlColIndex ]if (isUrl(url)) {return urlIsSuccess(url).then(res => {// 返回检测的状态码, 写入状态格rows[ statusColIndex ] = resreturn Promise.resolve(rows)})}return Promise.resolve(rows)
})Promise.all(promises).then((res) => {sheets[ sheetIndex ].data = res;const buffer = sheetsBuffer(sheets)writeFile(buffer, outFilePath)
}).catch(err => {console.log('err=', err)
})
操作 Excel 中数据
需要安装node-xlsx npm install node-xlsx --save
新建 xlsx.js
const fs = require("fs")
const xlsx = require('node-xlsx').default;
// 读取Excel表全部数据
const readSheets = (filePath) => {if (!filePath) return []return xlsx.parse(filePath)
}
// 读取Excel表中Sheet数据
const getSheet = (sheets, nameOrIndex) => {if (nameOrIndex === undefined) return sheetsif (typeof nameOrIndex === 'string') {return sheets.find(sheet => nameOrIndex === sheet.name)?.data}if (typeof nameOrIndex === 'number') {return sheets[ nameOrIndex ]?.data}throw new Error(`nameOrIndex 不合法: ${ nameOrIndex }`)
}const sheetsBuffer = (sheets) => {if (!sheets) {throw new Error(`sheets 参数是必填项`)}return xlsx.build(sheets)
};// 写入文件,需要提前手动创建
const writeFile = (buffer, outFilePath) => {fs.writeFile(outFilePath, buffer, function (err) {if (err) {console.log("写入失败: " + err);return;}console.log("写入完成");});
}module.exports = {readSheets,getSheet,sheetsBuffer,writeFile
}
检测 url 是否有效
使用 nodejs 的 child_process.exec(),调用 命令行curl命令, 根据返回的错误状态码或者HTTP状态码,判断 URL 是否有效
新建 urlIsSuccess.js
const child_process = require("child_process");const urlIsSuccess = (url) => {if (!url) {return Promise.resolve('')}// 微信内置浏览器 "Mozilla/5.0 AppleWebKit/600 Mobile MicroMessenger/6.0"const userAgent = '-A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"'const curl = `curl -I ${ userAgent } ${ url }`return new Promise((resolve, reject) => {child_process.exec(curl, function (err, stdout, stderr) {console.log(url, '完成')if (err) {resolve(err.code || '失败')return}let status = stdout.split('\n')if (status.length) {let code = status[ 0 ].match(/\s(\d{3})\s/)if (code && code[ 1 ]) {status = +code[ 1 ]}}if (!status) {status = stdout;}resolve(status || '成功')});})}module.exports = urlIsSuccess;
注意
有部分网站,做了人机检测, 需要对请求头等进行配置,否则通过curl访问会失败
Nodejs 批量检测 Excel 中url链接是否可访问相关推荐
- 批量下载Excel中的链接中的文件
前几天有一个小任务,就是把一个Excel中的三百多个音频下载下来,这些音频都是URL的形式,于是就写了个python脚本把他们都抓了下来,因为很简单,所以就直接贴代码了: 注意:首先要把Excel转换 ...
- 无需代码即可实现Excel中图片链接的批量下载
今天在使用腾讯文档收集截图时,发现导出的Excel表格中是一列图片链接,不能直观的看到上传的图片,上网找了很多方法,但大多数是通过敲代码的形式来实现图片链接的批量下载的,对于编程不厉害的我来说,实现起 ...
- 批量提取excel中包含特定关键字的单元格内容
批量提取excel中包含特定关键字的单元格内容 链接:https://pan.baidu.com/s/1uTnkYXh8jBcaeda-JoQDuQ 提取码:jbkv class get_xls_da ...
- python批量处理excel文本改为数字_Python批量修改Excel中的文件内容
import os import xlrd from xlutils.copy import copy def base_dir(filename=None): return os.path.join ...
- 如何批量隐藏Excel中手机号码四位数?
今天跟大家分享一下如何批量隐藏Excel中手机号码四位数? 1.打开Excel文件,如下图要求我们将Excel中多个手机号码批量隐藏中间四位数. 2.首先我们选中手机号码单元格区域 3.点击[diy工 ...
- Python之Excel编辑-[小试牛刀]批量替换excel中字符串
任务1:批量替换excel中指定字符串 解决思路: 逐个cell遍历Excel表格,进行字符串比对,找到匹配的cell,并将cell内容替换成新字符串 app = xw.App(visible=Tru ...
- python自动化办公(第9课)批量调整Excel中字体、样式
python自动化办公(第9课)批量调整Excel中字体.样式 - 修改字体样式 Font(name="字体名",size=字体大小,bold=是否加粗,italic=是否斜体,c ...
- php去掉excel看不见的空格,如何批量去除Excel中无法看不见的空格?
Excel技巧:如何批量去除Excel中无法看不见的空格? 场景:企业人事.财务.市场部等需要数据整理的办公人士. 问题:如何批量去除Excel中无法看不见的空格? 解答:利用替换和专门"清 ...
- 工作小技巧—在word中使用邮件批量插入excel中的数据
工作小技巧-在word中使用邮件批量插入excel中的数据 word中使用邮件批量插入excel中的数据 1.选择数据源 2.插入数据 3.完成合并 word中使用邮件批量插入excel中的数据 1. ...
- 利用for循环、openpyxl批量新建Excel中的表格
一.在Excel中批量创建工作表 要求实现的功能: 在指定的路径,新建一个Excel文件,该文件中有7月1日到7月31日共31个工作表. 准备: Python环境.安装openpyxl库 代码: im ...
最新文章
- MySQL Server has gone away报错原因汇总
- Python time mktime()方法
- nofollow标签_nofollow标签是什么?如何使用
- 使用蓝图构建Flask项目目录
- 开发环境配置--Ubuntu+Qt4+OpenCV(二)
- 基于深度学习的音乐推荐系统简述
- 【CTA系列】Kelly公式在最优f问题上的应用
- Ant design Vue 如何在a-table表格标题/内容上添加一个按钮
- MySQL使用Mycat实现分库分表-读写分离
- 打印ASCII码 c++
- 阿里云DataWorks介绍
- 使用form表单和table表格制作个人简历
- 视频批量添加透明水印,必须用这个方法
- PTA - 数据库合集19
- 写在工作的第十年: 谈一谈专注和基础的重要性
- 内容即广告是最好的移动商业模式?
- Java+JSP政府机关公文文档收发管理系统(含源码+论文+答辩PPT等)
- 计算机科学高校,科学网—中国顶尖计算机高校实力大起底
- android:Invalid file name: must contain only [a-z0-9_.]错误
- 新鲜!宁盾dKey手机动态口令牌简介