2026.06.04 | youres | 60次围观
在网站运维和SEO工作中,批量检测重定向是常规操作。但检测完之后,结果散落在终端输出里,不好整理、不好分析、不好汇报。把curl批量检测重定向的结果导出到Excel,能让巡检报告一目了然,也让历史数据可追溯、可对比。
本文介绍3种实用方法,从CSV到JSON再到直接生成xlsx文件,覆盖Linux和Windows两大平台。
方法一:curl输出CSV文件,Excel直接打开
CSV是最简单直接的方案。curl的-w参数配合格式化模板,把每次请求的关键信息(状态码、重定向次数、最终URL、耗时)输出为一行,多个URL的结果拼成CSV。
1. 创建格式化模板文件
先准备一个-w模板文件redirect.txt:
url_effective,http_code,num_redirects,time_total,time_redirect
2. 编写批量检测脚本
#!/bin/bash
# redirect_check.sh - 批量检测重定向并输出CSV
URLS_FILE="urls.txt"
OUTPUT="redirect_result.csv"
# 写入CSV表头
echo "原始URL,状态码,重定向次数,最终URL,总耗时(s),重定向耗时(s)" > "$OUTPUT"
while IFS= read -r url; do
[ -z "$url" ] && continue
result=$(curl -o /dev/null -s -L -w "%{http_code},%{num_redirects},%{url_effective},%{time_total},%{time_redirect}" "$url")
echo "$url,$result" >> "$OUTPUT"
done < "$URLS_FILE"
echo "结果已写入: $OUTPUT"
3. Excel打开CSV注意事项
- Linux/Mac生成的CSV默认UTF-8编码,Excel打开中文可能乱码,建议加BOM头:
printf '\xEF\xBB\xBF' | cat - "$OUTPUT" > tmp && mv tmp "$OUTPUT" - 字段含逗号时需要用双引号包裹
- 日期字段用ISO格式避免地区差异
方法二:输出JSON格式,灵活导入Excel
JSON比CSV更结构化,支持嵌套数据,适合复杂的巡检场景。用curl配合-w和jq组合,把每条检测结果转成JSON对象。
核心脚本
#!/bin/bash
# redirect_check_json.sh
URLS_FILE="urls.txt"
OUTPUT="redirect_result.json"
TIMESTAMP=$(date +%Y-%m-%dT%H:%M:%S)
echo '[' > "$OUTPUT"
first=true
while IFS= read -r url; do
[ -z "$url" ] && continue
if [ "$first" = true ]; then
first=false
else
echo ',' >> "$OUTPUT"
fi
result=$(curl -o /dev/null -s -L -w '{"code":%{http_code},"redirects":%{num_redirects},"final_url":"%{url_effective}","time_total":%{time_total},"time_redirect":%{time_redirect}}' "$url")
printf '{"url":"%s","timestamp":"%s","result":%s}' "$url" "$TIMESTAMP" "$result" >> "$OUTPUT"
done < "$URLS_FILE"
echo ']' >> "$OUTPUT"
echo "JSON结果已写入: $OUTPUT"
JSON转Excel方法
- Python方案:用
pandas.read_json()读取JSON,再to_excel()导出,支持自定义列名和样式 - 在线工具:json2csv.com等在线转换工具,适合偶尔使用
- PowerShell方案:
Get-Content | ConvertFrom-Json | Export-Excel(需安装ImportExcel模块)
方法三:直接生成xlsx文件(Python方案)
如果需要带样式的Excel报告(表头加粗、条件格式、自动列宽),Python的openpyxl是最佳选择。
完整脚本
#!/usr/bin/env python3
import subprocess
import openpyxl
from openpyxl.styles import Font, PatternFill, Alignment
from datetime import datetime
URLS_FILE = "urls.txt"
OUTPUT = "redirect_report.xlsx"
wb = openpyxl.Workbook()
ws = wb.active
ws.title = "重定向检测结果"
# 表头样式
header_font = Font(bold=True, color="FFFFFF")
header_fill = PatternFill(start_color="4472C4", end_color="4472C4", fill_type="solid")
headers = ["序号", "原始URL", "状态码", "重定向次数", "最终URL", "总耗时(s)", "重定向耗时(s)", "检测时间"]
for col, header in enumerate(headers, 1):
cell = ws.cell(row=1, column=col, value=header)
cell.font = header_font
cell.fill = header_fill
cell.alignment = Alignment(horizontal="center")
# 读取URL列表
with open(URLS_FILE, "r") as f:
urls = [line.strip() for line in f if line.strip()]
# 逐条检测
for idx, url in enumerate(urls, 1):
cmd = [
"curl", "-o", "/dev/null", "-s", "-L",
"-w", "%{http_code} %{num_redirects} %{url_effective} %{time_total} %{time_redirect}",
url
]
output = subprocess.run(cmd, capture_output=True, text=True).stdout.strip()
parts = output.split(" ")
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
row = [idx, url, parts[0], parts[1], parts[2], parts[3], parts[4], now]
for col, value in enumerate(row, 1):
ws.cell(row=idx + 1, column=col, value=value)
# 自动列宽
for col in ws.columns:
max_len = 0
for cell in col:
try:
max_len = max(max_len, len(str(cell.value)))
except:
pass
ws.column_dimensions[col[0].column_letter].width = min(max_len + 4, 60)
wb.save(OUTPUT)
print(f"Excel报告已生成: {OUTPUT}")
Windows用户注意事项
- 安装openpyxl:
pip install openpyxl - 如果curl不是GNU版本,用
curl.exe替代(Windows 10+自带) - 中文路径需要设置
encoding参数
3种方案对比
- CSV方案:最简单,零依赖,适合快速巡检。缺点是格式化能力弱,大数据量时Excel打开慢
- JSON方案:结构化好,方便程序处理和API对接。缺点是Excel不能直接打开,需要二次转换
- xlsx方案:最专业,支持样式、多Sheet、条件格式。缺点是需要Python环境和依赖
实际工作中推荐CSV做日常快速巡检,xlsx做周报/月报,JSON做数据备份和程序对接,三者配合使用效果最好。
相关文章推荐
版权声明
本文仅代表个人观点。
本文系AI辅助作者原创,未经许可,转载请保留原文链接。

发表评论