OpenClaw 数据分析实战指南
数据分析是 AI 助理的核心能力之一。通过整合数据收集、处理、可视化,OpenClaw 可以帮助你快速洞察数据价值。本文通过 4 个实战案例,详细讲解数据分析的完整流程。
一、工具概述
数据分析能力
- 数据收集:API 调用、文件读取、网页抓取
- 数据处理:清洗、转换、聚合
- 数据可视化:图表生成、报告输出
- 洞察提取:趋势分析、异常检测
二、实战案例 1:销售数据分析
场景说明
分析月度销售数据,找出销售趋势和异常情况。
完整实现
javascript
// 1. 读取销售数据
sales_data = read(path="./sales-2026-03.csv")
// 2. 解析 CSV
const rows = sales_data.split('\n').slice(1)
const products = []
for (const row of rows) {
const [date, product, amount, quantity] = row.split(',')
if (date && product) {
products.push({
date,
product,
amount: parseFloat(amount),
quantity: parseInt(quantity)
})
}
}
// 3. 计算统计指标
const totalAmount = products.reduce((sum, p) => sum + p.amount, 0)
const totalQuantity = products.reduce((sum, p) => sum + p.quantity, 0)
const avgAmount = totalAmount / products.length
// 4. 按产品分组
const byProduct = {}
for (const p of products) {
if (!byProduct[p.product]) {
byProduct[p.product] = { amount: 0, quantity: 0 }
}
byProduct[p.product].amount += p.amount
byProduct[p.product].quantity += p.quantity
}
// 5. 找出异常值
const outliers = products.filter(p => p.amount > avgAmount * 2)
// 6. 生成报告
report = \`
# 销售数据分析报告
## 总体统计
- 总销售额:¥\${totalAmount.toLocaleString()}
- 总销量:\${totalQuantity} 件
- 平均订单:¥\${avgAmount.toFixed(2)}
- 订单数量:\${products.length}
## 产品排名
\${Object.entries(byProduct)
.sort((a, b) => b[1].amount - a[1].amount)
.slice(0, 5)
.map(([name, data], i) =>
\`\${i+1}. \${name}: ¥\${data.amount.toLocaleString()} (\${data.quantity}件)\`
).join('\\n')}
## 异常订单
\${outliers.map(o =>
\`- \${o.date} \${o.product}: ¥\${o.amount.toLocaleString()}\`
).join('\\n')}
## 建议
1. 重点关注前 5 名产品
2. 分析异常订单原因
3. 优化低销量产品
\`
write(path="./sales-report.md", content=report)输出示例
# 销售数据分析报告
## 总体统计
- 总销售额:¥125,430
- 总销量:1,250 件
- 平均订单:¥418.10
- 订单数量:300
## 产品排名
1. OpenClaw 专业版:¥45,000 (150 件)
2. 企业版授权:¥32,500 (65 件)
3. ...
## 异常订单
- 2026-03-15 企业版授权:¥15,000
- 2026-03-20 专业版:¥12,000三、实战案例 2:网站流量分析
场景说明
分析网站访问日志,找出流量高峰和用户行为。
完整实现
javascript
// 1. 读取访问日志
log_data = read(path="./access.log")
// 2. 解析日志
const visits = []
for (const line of log_data.split('\n')) {
const match = line.match(/(\d{2}:\d{2}:\d{2}).*GET\s+(\/\S+).*"(\d{3})"/)
if (match) {
visits.push({
time: match[1],
path: match[2],
status: parseInt(match[3])
})
}
}
// 3. 按小时统计
const byHour = {}
for (const v of visits) {
const hour = v.time.split(':')[0]
byHour[hour] = (byHour[hour] || 0) + 1
}
// 4. 找出高峰时段
const peakHour = Object.entries(byHour)
.sort((a, b) => b[1] - a[1])[0]
// 5. 统计页面访问
const byPath = {}
for (const v of visits) {
byPath[v.path] = (byPath[v.path] || 0) + 1
}
// 6. 生成报告
report = \`
# 网站流量分析报告
## 总体统计
- 总访问量:\${visits.length}
- 高峰时段:\${peakHour[0]}:00 (\${peakHour[1]} 次访问)
## 热门页面
\${Object.entries(byPath)
.sort((a, b) => b[1] - a[1])
.slice(0, 10)
.map(([path, count], i) =>
\`\${i+1}. \${path}: \${count} 次\`
).join('\\n')}
## 时段分布
\${Object.entries(byHour)
.sort((a, b) => a[0] - b[0])
.map(([hour, count]) =>
\`\${hour}:00 - \${parseInt(hour)+1}:00: \${count} 次 \${'█'.repeat(count/10)}\`
).join('\\n')}
\`
write(path="./traffic-report.md", content=report)四、实战案例 3:用户反馈分析
场景说明
分析用户反馈,提取关键词和情感倾向。
完整实现
javascript
// 1. 读取反馈数据
feedback_data = read(path="./feedback.json")
const feedbacks = JSON.parse(feedback_data)
// 2. 关键词提取
const keywords = {}
for (const f of feedbacks) {
const words = f.content.split(/[\s,,.。]+/)
for (const word of words) {
if (word.length > 1) {
keywords[word] = (keywords[word] || 0) + 1
}
}
}
// 3. 情感分析(简单版)
const positiveWords = ['好', '棒', '满意', '推荐', '优秀']
const negativeWords = ['差', '糟糕', '失望', '问题', '故障']
let positive = 0, negative = 0, neutral = 0
for (const f of feedbacks) {
let score = 0
positiveWords.forEach(w => { if (f.content.includes(w)) score++ })
negativeWords.forEach(w => { if (f.content.includes(w)) score-- })
if (score > 0) positive++
else if (score < 0) negative++
else neutral++
}
// 4. 生成报告
report = \`
# 用户反馈分析报告
## 总体统计
- 总反馈数:\${feedbacks.length}
- 正面:\${positive} (\${(positive/feedbacks.length*100).toFixed(1)}%)
- 中性:\${neutral} (\${(neutral/feedbacks.length*100).toFixed(1)}%)
- 负面:\${negative} (\${(negative/feedbacks.length*100).toFixed(1)}%)
## 高频关键词
\${Object.entries(keywords)
.sort((a, b) => b[1] - a[1])
.slice(0, 10)
.map(([word, count], i) =>
\`\${i+1}. \${word}: \${count} 次\`
).join('\\n')}
## 建议
1. 关注负面反馈,及时改进
2. 放大正面评价,用于宣传
3. 持续收集反馈,优化产品
\`
write(path="./feedback-report.md", content=report)五、高级技巧
1. 数据可视化
javascript
// 生成简单的文本图表
function drawBarChart(data, width=40) {
const max = Math.max(...Object.values(data))
return Object.entries(data)
.map(([label, value]) => {
const barLength = Math.round((value / max) * width)
const bar = '█'.repeat(barLength)
return \`\${label}: \${bar} (\${value})\`
})
.join('\\n')
}2. 数据导出
javascript
// 导出为 CSV
function exportToCSV(data, filename) {
const headers = Object.keys(data[0])
const csv = [
headers.join(','),
...data.map(row => headers.map(h => row[h]).join(','))
].join('\\n')
write(path=filename, content=csv)
}3. 数据对比
javascript
// 对比两个时期数据
function comparePeriods(current, previous) {
const changes = {}
for (const key of Object.keys(current)) {
const change = current[key] - previous[key]
const percent = ((change / previous[key]) * 100).toFixed(2)
changes[key] = { change, percent }
}
return changes
}六、常见问题
Q: 如何处理大数据集?
A: 分页读取,分批处理,避免内存溢出。
Q: 如何保证数据准确性?
A: 数据验证、异常检测、交叉验证。
Q: 如何自动化报告生成?
A: 使用定时任务,定期执行分析脚本。
七、总结
数据分析帮助我们从数据中提取价值,支持决策。
相关资源: