utility2026-07-253 分钟阅读
什么是 User-Agent 解析
User-Agent 解析是将浏览器发送的 User-Agent 字符串解析为结构化信息(浏览器、版本、操作系统、设备类型等)的过程。
User-Agent 字符串结构
Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X)
AppleWebKit/605.1.15 (KHTML, like Gecko)
Version/16.0 Mobile/15E148 Safari/604.1
│ │ │ │
│ │ │ └─ 浏览器版本
│ │ └─ 浏览器名称
│ └─ 操作系统
└─ 引擎版本
解析结果示例
{
"browser": {
"name": "Safari",
"version": "16.0",
"engine": "WebKit",
"engineVersion": "605.1.15"
},
"os": {
"name": "iOS",
"version": "16.0",
"platform": "iPhone"
},
"device": {
"type": "mobile",
"brand": "Apple",
"model": "iPhone"
},
"isMobile": true,
"isTablet": false,
"isDesktop": false
}
为什么需要 User-Agent 解析
- 数据分析:统计用户浏览器和设备分布
- 兼容性测试:识别旧版浏览器并提供降级方案
- 响应式设计:根据设备类型调整界面
- 安全分析:检测异常或伪造的 User-Agent
- SEO 优化:为不同设备提供优化内容
- 广告定向:基于设备类型投放广告
如何使用在线工具
使用 DevToolkit Pro 的 User-Agent 解析器:
- 粘贴完整的 User-Agent 字符串
- 自动解析为结构化 JSON
- 显示浏览器、OS、设备详细信息
- 标记移动端/桌面端/平板
- 一键复制解析结果
代码中的解析
JavaScript
function parseUserAgent(ua) {
const result = {
browser: { name: 'Unknown', version: '' },
os: { name: 'Unknown', version: '' },
device: { type: 'desktop' },
};
// 检测操作系统
if (/iPhone|iPad|iPod/.test(ua)) {
result.os.name = 'iOS';
result.os.platform = /iPhone/.test(ua) ? 'iPhone' : 'iPad';
result.device.type = /iPad/.test(ua) ? 'tablet' : 'mobile';
} else if (/Windows/.test(ua)) {
result.os.name = 'Windows';
const match = ua.match(/Windows NT (\d+\.\d+)/);
if (match) result.os.version = match[1];
} else if (/Mac OS X/.test(ua)) {
result.os.name = 'macOS';
const match = ua.match(/Mac OS X (\d+[._]\d+)/);
if (match) result.os.version = match[1].replace('_', '.');
} else if (/Android/.test(ua)) {
result.os.name = 'Android';
result.device.type = /Mobile/.test(ua) ? 'mobile' : 'tablet';
} else if (/Linux/.test(ua)) {
result.os.name = 'Linux';
}
// 检测浏览器
if (/Chrome/.test(ua) && !/Edg/.test(ua)) {
result.browser.name = 'Chrome';
const match = ua.match(/Chrome\/(\d+\.\d+)/);
if (match) result.browser.version = match[1];
} else if (/Safari/.test(ua) && !/Chrome/.test(ua)) {
result.browser.name = 'Safari';
const match = ua.match(/Version\/(\d+\.\d+)/);
if (match) result.browser.version = match[1];
} else if (/Firefox/.test(ua)) {
result.browser.name = 'Firefox';
const match = ua.match(/Firefox\/(\d+\.\d+)/);
if (match) result.browser.version = match[1];
} else if (/Edg/.test(ua)) {
result.browser.name = 'Edge';
const match = ua.match(/Edg\/(\d+\.\d+)/);
if (match) result.browser.version = match[1];
}
return result;
}
Python
import re
def parse_user_agent(ua):
result = {
'browser': {'name': 'Unknown', 'version': ''},
'os': {'name': 'Unknown', 'version': ''},
'device': {'type': 'desktop'}
}
# 检测操作系统
if 'iPhone' in ua or 'iPad' in ua:
result['os']['name'] = 'iOS'
result['device']['type'] = 'mobile' if 'iPhone' in ua else 'tablet'
elif 'Windows' in ua:
result['os']['name'] = 'Windows'
match = re.search(r'Windows NT (\d+\.\d+)', ua)
if match: result['os']['version'] = match.group(1)
elif 'Mac OS X' in ua:
result['os']['name'] = 'macOS'
elif 'Android' in ua:
result['os']['name'] = 'Android'
result['device']['type'] = 'mobile' if 'Mobile' in ua else 'tablet'
# 检测浏览器
if 'Chrome' in ua and 'Edg' not in ua:
result['browser']['name'] = 'Chrome'
elif 'Safari' in ua and 'Chrome' not in ua:
result['browser']['name'] = 'Safari'
elif 'Firefox' in ua:
result['browser']['name'] = 'Firefox'
return result
FAQ
User-Agent 字符串会被弃用吗?
是的,Google Chrome 正在推进 User-Agent 减少计划(User-Agent Reduction)。未来只会发送简化的 UA 信息。建议使用 Client Hints API 作为替代。
如何检测机器人和爬虫?
检查 UA 中是否包含 bot、crawler、spider 等关键词。但注意:恶意爬虫可以伪造 UA,需要结合其他特征(请求频率、行为模式)判断。
移动端和桌面端如何区分?
主要依据:1) UA 中是否包含 Mobile/Android/iPhone 等关键词;2) 屏幕分辨率(< 768px 通常为移动端);3) 触摸支持(ontouchstart)。
本文由 DevToolkit Pro 提供。更多开发者工具请访问 首页。