主题
通过 ID 获取 Lighthouse 审计结果(JSON)
接口说明
该接口用于根据任务 ID 获取 Lighthouse 审计结果。
OnPage Lighthouse 功能基于 Google 开源的 Lighthouse 项目,用于评估网页或 Web App 的质量表现。你需要调用 /v3/on_page/lighthouse/task_post/ 创建任务,再使用返回结果中的 id 调用本接口拉取审计结果。
返回会你在任务创建时请求的分类(categories)和审计项(audits)数据。默认,结果通常覆盖页面的性能、可访问性、PWA、SEO 以及最佳实践等信息。
注意:
- 如果某个 audit 名称中斜杠
/,请在audits对象中使用斜杠后的最后一段名称进行检索。result中的字段结构遵循 Lighthouse 官方输出格式,本平台保持平台容。
请求地址
GET https://api.seermartech.cn/v3/on_page/lighthouse/task_get/json/$id
计费说明
账号在创建任务时扣费,获取结果本身不额外计费。 扣费以响应头 X-SeerMarTech-Charge-CNY 为准。
路径参数
| 字段 | 类型 | 填 | 说明 |
|---|---|---|---|
id | string | 是 | 任务唯一标识符,UUID 格式。可从 /v3/on_page/lighthouse/task_post/ 的响应中获取。示例:07131248-1535-0216-1000-17384017ad04 |
返回结构
接口返回 JSON 数据,顶层 tasks 数组。
顶层字段
| 字段 | 类型 | 说明 |
|---|---|---|
version | string | API 当前版本 |
status_code | integer | 通用状态码 |
status_message | string | 通用状态信息 |
time | string | 执行耗时,单位秒 |
cost | float | 本次请求对应任务总成本 |
tasks_count | integer | tasks 数组中的任务数量 |
tasks_error | integer | 返回错误的任务数量 |
tasks | array | 任务结果列表 |
tasks[] 字段
| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 任务 ID,UUID 格式 |
status_code | integer | 任务状态码,范围通常为 10000-60000 |
status_message | string | 任务状态说明 |
time | string | 任务执行耗时 |
cost | float | 该任务成本 |
result_count | integer | result 数组数量 |
path | array | 请求路径 |
data | object | 与任务提交时一致的请求参数 |
result | array | Lighthouse 审计结果 |
data 对象常见字段
| 字段 | 类型 | 说明 |
|---|---|---|
api | string | API 模块,通常为 on_page |
function | string | 功能名称,通常为 lighthouse |
url | string | 被审计页面 URL |
for_mobile | boolean | 是否按移动端模式执行 |
categories | array | 请求时指定的分类 |
result[] 说明
result 数组中的单个对象为 Lighthouse 标准结果,常见字段:
| 字段 | 类型 | 说明 |
|---|---|---|
userAgent | string | 审计使用的浏览器 UA |
environment | object | Lighthouse 运行环境信息 |
audits | object | 各项审计明细 |
configSettings | object | 本次 Lighthouse置 |
categories | object | 分类分数,如 performance、seo |
categoryGroups | object | 分类分组说明 |
stackPacks | array | 针对特定技术栈的优化建议 |
entities | array | 页面涉及的第三方实体信息 |
fullPageScreenshot | object | 页面整页截图及节点位置信息 |
timing | object | 审计各阶段时间信息 |
i18n | object | 化文案与字段映射信息 |
常见审计字段说明
在 result[0].audits 中,通常可以看到如下审计项:
| Audit ID | 含义 |
|---|---|
first-contentful-paint | 首次绘制时间(FCP) |
largest-contentful-paint | 最大绘制时间(LCP) |
speed-index | 速度指数 |
total-blocking-time | 总阻塞时间(TBT) |
max-potential-fid | 最大潜在首次延迟 |
cumulative-layout-shift | 累积布局偏移(CLS) |
interactive | 可交互时间(TTI) |
server-response-time | 首/服务器响应时间 |
network-requests | 页面加载期间的网络请求明细 |
resource-summary | 资源汇总 |
unused-css-rules | 未使用 CSS |
unused-javascript | 未使用 JavaScript |
render-blocking-insight | 渲染阻塞请求 |
image-delivery-insight | 图片传输优化建议 |
third-parties-insight | 第三方资源影响 |
不同任务下,返回的审计项数量和字段会有所不同。
状态码与错误处理
顶层状态码
20000:请求成功- 状态码:表示请求级异常、参数错误、认证失败或服务端问题
任务级状态码
每个 tasks[]素都有独立的 status_code 和 status_message。 即使顶层请求成功,也应继续检查每个任务的状态。
建议:
- 判断顶层
status_code- 再遍历
tasks[]判断任务级状态- 若
taskResult.status_code >= 40000或result为空,应按失败处理
错误码请参考错误码附录。
请求示例
curl
bash
# 使用任务 ID 获取 Lighthouse 审计结果
id="07281559-0695-0216-0000-c269be8b7592"
curl --location --request GET "https://api.seermartech.cn/v3/on_page/lighthouse/task_get/json/${id}" \
--header "Authorization: Bearer smt_live_YOUR_KEY" \
--header "Content-Type: application/json"Python
python
import requests
# 任务 ID
task_id = "07281559-0695-0216-0000-c269be8b7592"
url = f"https://api.seermartech.cn/v3/on_page/lighthouse/task_get/json/{task_id}"
headers = {
"Authorization": "Bearer smt_live_YOUR_KEY",
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
data = response.json
# 检查顶层状态
if data.get("status_code") == 20000:
for task in data.get("tasks", []):
# 再检查任务级状态
if task.get("status_code", 0) >= 40000 or not task.get("result"):
print(f"任务失败: {task.get('status_code')} {task.get('status_message')}")
else:
print(task["result"][0])
else:
print(f"请求失败: {data.get('status_code')} {data.get('status_message')}")TypeScript
typescript
import axios from "axios";
const taskId = "02231934-2604-0066-2000-570459f04879";
axios({
method: "get",
url: `https://api.seermartech.cn/v3/on_page/lighthouse/task_get/json/${taskId}`,
headers: {
"Authorization": "Bearer smt_live_YOUR_KEY",
"Content-Type": "application/json"
}
}).then((response) => {
const data = response.data;
// 检查顶层状态码
if (data.status_code === 20000) {
for (const task of data.tasks || []) {
if (task.status_code >= 40000 || !task.result) {
console.log(`任务失败: ${task.status_code} ${task.status_message}`);
} else {
// Lighthouse 结果
console.log(task.result[0]);
}
}
} else {
console.log(`请求失败: ${data.status_code} ${data.status_message}`);
}
}).catch((error) => {
console.error(error);
});响应示例
下面示例为精简后的返回结构,便于理解字段层级:
json
{
"version": "0.1.20260318",
"status_code": 20000,
"status_message": "Ok.",
"time": "0.0919 sec.",
"cost": 0,
"tasks_count": 1,
"tasks_error": 0,
"tasks": [
{
"id": "07281559-0695-0216-0000-c269be8b7592",
"status_code": 20000,
"status_message": "Ok.",
"time": "0.0201 sec.",
"cost": 0,
"result_count": 1,
"path": [
"v3",
"on_page",
"lighthouse",
"task_get",
"json",
"07281559-0695-0216-0000-c269be8b7592"
],
"data": {
"api": "on_page",
"function": "lighthouse",
"url": "https://example.com",
"for_mobile": false,
"categories": [
"performance"
]
},
"result": [
{
"userAgent": "Mozilla/5.0 ...",
"environment": {
"networkUserAgent": "Mozilla/5.0 ...",
"hostUserAgent": "Mozilla/5.0 ...",
"benchmarkIndex": 3002.5
},
"audits": {
"first-contentful-paint": {
"id": "first-contentful-paint",
"title": "First Contentful Paint",
"score": 0.98,
"numericValue": 694.264,
"numericUnit": "millisecond",
"displayValue": "0.7 s"
},
"largest-contentful-paint": {
"id": "largest-contentful-paint",
"title": "Largest Contentful Paint",
"score": 0.87,
"numericValue": 1284.36,
"numericUnit": "millisecond",
"displayValue": "1.3 s"
},
"speed-index": {
"id": "speed-index",
"title": "Speed Index",
"score": 0.64,
"numericValue": 1946.3868,
"numericUnit": "millisecond",
"displayValue": "1.9 s"
},
"total-blocking-time": {
"id": "total-blocking-time",
"title": "Total Blocking Time",
"score": 1,
"numericValue": 0,
"numericUnit": "millisecond",
"displayValue": "0 ms"
},
"cumulative-layout-shift": {
"id": "cumulative-layout-shift",
"title": "Cumulative Layout Shift",
"score": 1,
"numericValue": 0.009361777427545749,
"numericUnit": "unitless",
"displayValue": "0.009"
}
},
"configSettings": {
"formFactor": "desktop",
"throttlingMethod": "simulate",
"screenEmulation": {
"mobile": false,
"width": 1350,
"height": 940
},
"locale": "en-US"
},
"categories": {
"performance": {
"title": "Performance",
"id": "performance",
"score": 0.93
}
},
"fullPageScreenshot": {
"screenshot": {
"data": "data:image/webp;base64,...",
"width": 1350,
"height": 8128
}
},
"timing": {
"total": 16868.260000000002
}
}
]
}
]
}响应解读建议
1. 读取分类分数
如果你心整体表现,可优查看:
result[0].categories.performance.scoreresult[0].categories.seo.scoreresult[0].categories.accessibility.scoreresult[0].categories.best-practices.score
2. 读取核心性能指标
重点:
first-contentful-paintlargest-contentful-painttotal-blocking-timecumulative-layout-shiftinteractive
这些字段一般位于:
result[0].audits.{audit_id}
3. 提取优化建议
若用于技术整改,可进一步读取:
unused-css-rulesunused-javascriptrender-blocking-insightimage-delivery-insightthird-parties-insightnetwork-dependency-tree-insight
这类字段通常:
scoredisplayValuedetailsmetricSavingsguidanceLevel
4. 获取截图或页面节点定位
如需做可视化诊断,可用:
fullPageScreenshot.screenshot.datafullPageScreenshot.nodes
使用注意事项
- 本接口使用任务提交后返回的 ID 查询。
- 若任务尚未完成,可能无法返回完整
result。 - Lighthouse 返回数据体积可能很大,在:
- 截图
- 完整资源树
- 第三方实体
- Trace / i18n 信息 时更明显,建议按需存储和解析。
- 如果心性能评分,建议只提取字段,保存整个原始结果。
- 由于 Lighthouse 输出结构会随平台版本迭代,解析时建议采用健壮的字段存在性判断。
实用场景
- 诊断页面性能瓶颈:提取 FCP、LCP、TBT、CLS 等核心指标,快速定位影响加载与交互体验的问题页面。
- 监控技术 SEO 健康度:结合 Lighthouse 的 SEO 分类与 audits,发现移动适、索引可访问性和结构化问题。
- 输出开发整改单:基于未使用 CSS、未使用 JavaScript、渲染阻塞资源等字段,自动生成前端优化任务。
- 做竞品页面体验对比:对不同站点执行同类审计并比较性能分类分数,为 SEO 与转化优化提供依据。
- 沉淀页面质量档案:将 Lighthouse 审计结果按页面、目录或站点维度归档,长期跟踪版本发布后的质量变化。