# ua-device [![Build Status](https://travis-ci.com/kaivean/python-ua-device.svg?branch=master)](https://travis-ci.com/kaivean/python-ua-device)
解析user-agent的python包,可以获取到系统、浏览器内核、浏览器、设备信息,其特点:
* 相比国内外的流行的python包,该模块解析国内复杂的ua信息更加精确,有几千行代码专门来匹配具体的特定的ua
## Why
由于在国内生产PC的厂家有限,大众用户使用的浏览器也主要是当前的一些主流浏览器。因此目前的UA解析库在对OS、浏览器外壳、浏览器内核等的识别率都相当高。但是由于国内的移动设备的五花八门,对于移动设备的硬件信息是很难用一套通用的方法进行识别,因此 ua-device 诞生
* 通过机型识别品牌: 例如 [-\s](Galaxy[\s-_]nexus|Galaxy[\s-_]\w*[\s-_]\w*|Galaxy[\s-_]\w*|SM-\w*|GT-\w*|s[cgp]h-\w*|shw-\w* 这样的匹配规则以及一些从中关村在线爬取到的机型名称如G3508、G3508J、G3508i 等识别出来该机型的品牌为Samsung 因为单纯从UA信息确实无法得到品牌数据,这也是为何很多高Star的UA解析库识别手机品牌成功率只有30%-40%的原因(ua-device识别率可见下面测试用例)。
* 解决国内UA信息不规范: 由于国内很多手机生产厂家的设计问题,例如小米可供识别的UA数据可能为 mi 2 、mi2、m2、mi-2LTE、MI-20150XX、minote等等,如果匹配规则限制太紧就会导致数据无法命中,如果匹配规则太松又会让其它山寨机型滥竽充数,所以需要一套比较特殊的处理流程。
* 解决国内因不同发版而造成的UA数据不一致: 例如很多机型会因同电信、移动、联通而UA信息不同,但实际应该把他们算成同一款手机
* 解决机型的重命名与合并: 很多手机在不同时间生产其UA信息可能不同,所以需要对他们进行合并,防止在展示top数据时因数据分散而排不上号。
## 解析成功率
供参考(以30000个线上ua测试):
* 浏览器:98.5%
* 系统: 99.8%
* 内核: 99.92%
* 设备类型: 100%
* 设备型号:98.9%
* 厂商信息:95.5%
## javascript版本
[ua-device](https://github.com/fex-team/ua-device)
## 安装
```bash
pip install -U uaDevice
```
## 使用
```python
import uaDevice
ua='Mozilla/5.0 (iPhone; CPU iPhone OS 12_0_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 MQQBrowser/8.9.1 Mobile/15E148 Safari/604.1 MttCustomUA/2 QBWebViewType/1 WKType/1'
info = uaDevice.parseUA(ua)
output = '\t'.join([
info['os']['name'],
info['os']['version']['original'],
info['browser']['name'],
info['browser']['version']['original'],
info['engine']['name'],
info['engine']['version']['original'],
info['device']['type'],
info['device']['model'],
info['device']['manufacturer']
])
print(output)
```
## 贡献
随着新设备新app等等的上市,ua信息会越来越复杂,因为该项目需要不断迭代,希望大家一起来贡献不支持的ua,使得ua解析越来越准确
## 感谢
该项目是基于fex团队维护的js版本的ua解析库[ua-device](https://github.com/fex-team/ua-device), 在此表示感谢
Raw data
{
"_id": null,
"home_page": "https://github.com/kaivean/python-ua-device",
"name": "uaDevice",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "ua, user-agent, User Agent, parser, device, os, browser, engine, data analysis, china, \u4e2d\u56fd, \u56fd\u5185",
"author": "kaivean",
"author_email": "kaivean@outlook.com",
"download_url": "https://files.pythonhosted.org/packages/7f/5d/8083574078c25a90e060942e55092fbb38338498ff251465aecf52635c3d/uaDevice-1.0.12.tar.gz",
"platform": "any",
"description": "# ua-device [![Build Status](https://travis-ci.com/kaivean/python-ua-device.svg?branch=master)](https://travis-ci.com/kaivean/python-ua-device)\n\u89e3\u6790user-agent\u7684python\u5305\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230\u7cfb\u7edf\u3001\u6d4f\u89c8\u5668\u5185\u6838\u3001\u6d4f\u89c8\u5668\u3001\u8bbe\u5907\u4fe1\u606f\uff0c\u5176\u7279\u70b9\uff1a\n\n* \u76f8\u6bd4\u56fd\u5185\u5916\u7684\u6d41\u884c\u7684python\u5305\uff0c\u8be5\u6a21\u5757\u89e3\u6790\u56fd\u5185\u590d\u6742\u7684ua\u4fe1\u606f\u66f4\u52a0\u7cbe\u786e\uff0c\u6709\u51e0\u5343\u884c\u4ee3\u7801\u4e13\u95e8\u6765\u5339\u914d\u5177\u4f53\u7684\u7279\u5b9a\u7684ua\n\n## Why\n\n\u7531\u4e8e\u5728\u56fd\u5185\u751f\u4ea7PC\u7684\u5382\u5bb6\u6709\u9650\uff0c\u5927\u4f17\u7528\u6237\u4f7f\u7528\u7684\u6d4f\u89c8\u5668\u4e5f\u4e3b\u8981\u662f\u5f53\u524d\u7684\u4e00\u4e9b\u4e3b\u6d41\u6d4f\u89c8\u5668\u3002\u56e0\u6b64\u76ee\u524d\u7684UA\u89e3\u6790\u5e93\u5728\u5bf9OS\u3001\u6d4f\u89c8\u5668\u5916\u58f3\u3001\u6d4f\u89c8\u5668\u5185\u6838\u7b49\u7684\u8bc6\u522b\u7387\u90fd\u76f8\u5f53\u9ad8\u3002\u4f46\u662f\u7531\u4e8e\u56fd\u5185\u7684\u79fb\u52a8\u8bbe\u5907\u7684\u4e94\u82b1\u516b\u95e8\uff0c\u5bf9\u4e8e\u79fb\u52a8\u8bbe\u5907\u7684\u786c\u4ef6\u4fe1\u606f\u662f\u5f88\u96be\u7528\u4e00\u5957\u901a\u7528\u7684\u65b9\u6cd5\u8fdb\u884c\u8bc6\u522b\uff0c\u56e0\u6b64 ua-device \u8bde\u751f\n\n* \u901a\u8fc7\u673a\u578b\u8bc6\u522b\u54c1\u724c: \u4f8b\u5982 [-\\s](Galaxy[\\s-_]nexus|Galaxy[\\s-_]\\w*[\\s-_]\\w*|Galaxy[\\s-_]\\w*|SM-\\w*|GT-\\w*|s[cgp]h-\\w*|shw-\\w* \u8fd9\u6837\u7684\u5339\u914d\u89c4\u5219\u4ee5\u53ca\u4e00\u4e9b\u4ece\u4e2d\u5173\u6751\u5728\u7ebf\u722c\u53d6\u5230\u7684\u673a\u578b\u540d\u79f0\u5982G3508\u3001G3508J\u3001G3508i \u7b49\u8bc6\u522b\u51fa\u6765\u8be5\u673a\u578b\u7684\u54c1\u724c\u4e3aSamsung \u56e0\u4e3a\u5355\u7eaf\u4eceUA\u4fe1\u606f\u786e\u5b9e\u65e0\u6cd5\u5f97\u5230\u54c1\u724c\u6570\u636e\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4f55\u5f88\u591a\u9ad8Star\u7684UA\u89e3\u6790\u5e93\u8bc6\u522b\u624b\u673a\u54c1\u724c\u6210\u529f\u7387\u53ea\u670930%-40%\u7684\u539f\u56e0(ua-device\u8bc6\u522b\u7387\u53ef\u89c1\u4e0b\u9762\u6d4b\u8bd5\u7528\u4f8b)\u3002\n* \u89e3\u51b3\u56fd\u5185UA\u4fe1\u606f\u4e0d\u89c4\u8303: \u7531\u4e8e\u56fd\u5185\u5f88\u591a\u624b\u673a\u751f\u4ea7\u5382\u5bb6\u7684\u8bbe\u8ba1\u95ee\u9898\uff0c\u4f8b\u5982\u5c0f\u7c73\u53ef\u4f9b\u8bc6\u522b\u7684UA\u6570\u636e\u53ef\u80fd\u4e3a mi 2 \u3001mi2\u3001m2\u3001mi-2LTE\u3001MI-20150XX\u3001minote\u7b49\u7b49\uff0c\u5982\u679c\u5339\u914d\u89c4\u5219\u9650\u5236\u592a\u7d27\u5c31\u4f1a\u5bfc\u81f4\u6570\u636e\u65e0\u6cd5\u547d\u4e2d\uff0c\u5982\u679c\u5339\u914d\u89c4\u5219\u592a\u677e\u53c8\u4f1a\u8ba9\u5176\u5b83\u5c71\u5be8\u673a\u578b\u6ee5\u7afd\u5145\u6570\uff0c\u6240\u4ee5\u9700\u8981\u4e00\u5957\u6bd4\u8f83\u7279\u6b8a\u7684\u5904\u7406\u6d41\u7a0b\u3002\n* \u89e3\u51b3\u56fd\u5185\u56e0\u4e0d\u540c\u53d1\u7248\u800c\u9020\u6210\u7684UA\u6570\u636e\u4e0d\u4e00\u81f4: \u4f8b\u5982\u5f88\u591a\u673a\u578b\u4f1a\u56e0\u540c\u7535\u4fe1\u3001\u79fb\u52a8\u3001\u8054\u901a\u800cUA\u4fe1\u606f\u4e0d\u540c\uff0c\u4f46\u5b9e\u9645\u5e94\u8be5\u628a\u4ed6\u4eec\u7b97\u6210\u540c\u4e00\u6b3e\u624b\u673a\n* \u89e3\u51b3\u673a\u578b\u7684\u91cd\u547d\u540d\u4e0e\u5408\u5e76: \u5f88\u591a\u624b\u673a\u5728\u4e0d\u540c\u65f6\u95f4\u751f\u4ea7\u5176UA\u4fe1\u606f\u53ef\u80fd\u4e0d\u540c\uff0c\u6240\u4ee5\u9700\u8981\u5bf9\u4ed6\u4eec\u8fdb\u884c\u5408\u5e76\uff0c\u9632\u6b62\u5728\u5c55\u793atop\u6570\u636e\u65f6\u56e0\u6570\u636e\u5206\u6563\u800c\u6392\u4e0d\u4e0a\u53f7\u3002\n\n## \u89e3\u6790\u6210\u529f\u7387\n\u4f9b\u53c2\u8003\uff08\u4ee530000\u4e2a\u7ebf\u4e0aua\u6d4b\u8bd5\uff09:\n* \u6d4f\u89c8\u5668\uff1a98.5%\n* \u7cfb\u7edf\uff1a 99.8%\n* \u5185\u6838\uff1a 99.92%\n* \u8bbe\u5907\u7c7b\u578b\uff1a 100%\n* \u8bbe\u5907\u578b\u53f7\uff1a98.9%\n* \u5382\u5546\u4fe1\u606f\uff1a95.5%\n\n## javascript\u7248\u672c\n[ua-device](https://github.com/fex-team/ua-device)\n\n\n## \u5b89\u88c5\n\n```bash\npip install -U uaDevice\n```\n\n## \u4f7f\u7528\n\n```python\nimport uaDevice\nua='Mozilla/5.0 (iPhone; CPU iPhone OS 12_0_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 MQQBrowser/8.9.1 Mobile/15E148 Safari/604.1 MttCustomUA/2 QBWebViewType/1 WKType/1'\ninfo = uaDevice.parseUA(ua)\noutput = '\\t'.join([\n info['os']['name'],\n info['os']['version']['original'],\n info['browser']['name'],\n info['browser']['version']['original'],\n info['engine']['name'],\n info['engine']['version']['original'],\n info['device']['type'],\n info['device']['model'],\n info['device']['manufacturer']\n])\nprint(output)\n```\n\n## \u8d21\u732e\n\u968f\u7740\u65b0\u8bbe\u5907\u65b0app\u7b49\u7b49\u7684\u4e0a\u5e02\uff0cua\u4fe1\u606f\u4f1a\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u56e0\u4e3a\u8be5\u9879\u76ee\u9700\u8981\u4e0d\u65ad\u8fed\u4ee3\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u6765\u8d21\u732e\u4e0d\u652f\u6301\u7684ua\uff0c\u4f7f\u5f97ua\u89e3\u6790\u8d8a\u6765\u8d8a\u51c6\u786e\n\n## \u611f\u8c22\n\u8be5\u9879\u76ee\u662f\u57fa\u4e8efex\u56e2\u961f\u7ef4\u62a4\u7684js\u7248\u672c\u7684ua\u89e3\u6790\u5e93[ua-device](https://github.com/fex-team/ua-device)\uff0c \u5728\u6b64\u8868\u793a\u611f\u8c22\n\n",
"bugtrack_url": null,
"license": "MIT Licence",
"summary": "User Agent parser, More accurate",
"version": "1.0.12",
"project_urls": {
"Homepage": "https://github.com/kaivean/python-ua-device"
},
"split_keywords": [
"ua",
" user-agent",
" user agent",
" parser",
" device",
" os",
" browser",
" engine",
" data analysis",
" china",
" \u4e2d\u56fd",
" \u56fd\u5185"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "7f5d8083574078c25a90e060942e55092fbb38338498ff251465aecf52635c3d",
"md5": "4a00db6667f2ab955f465754f2ad4d45",
"sha256": "61896f6ee5b64dc6ff93943f4d348fe0cd8162e330ce75589b48dc145309ecf9"
},
"downloads": -1,
"filename": "uaDevice-1.0.12.tar.gz",
"has_sig": false,
"md5_digest": "4a00db6667f2ab955f465754f2ad4d45",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 47902,
"upload_time": "2024-11-07T07:06:15",
"upload_time_iso_8601": "2024-11-07T07:06:15.050733Z",
"url": "https://files.pythonhosted.org/packages/7f/5d/8083574078c25a90e060942e55092fbb38338498ff251465aecf52635c3d/uaDevice-1.0.12.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-07 07:06:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "kaivean",
"github_project": "python-ua-device",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"lcname": "uadevice"
}