解析 HTTP 用户代理字符串
问题描述
在 Python 中解析 User-Agent 字符串以可靠检测的最佳方法是什么
What is the best method to parse a User-Agent string in Python to reliably detect
- 浏览器
- 浏览器版本
- 操作系统
或者可能是任何可以做到这一点的帮助库
Or perhaps any helper library that does it
解决方案
我终于决定自己写了,我对结果很满意.请随时使用/修改/给我发送补丁等.
I finally decided to write my own, and I am happy with the outcome. Please feel free to use/modify/send me patches, etc.
在这里:http://pypi.python.org/pypi/httpagentparser
使用示例:
>>> import httpagentparser
>>> s = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/532.9 (KHTML, like Gecko)
Chrome/5.0.307.11 Safari/532.9"
>>> print(httpagentparser.simple_detect(s))
('Linux', 'Chrome 5.0.307.11')
>>> print(httpagentparser.detect(s))
{'os': {'name': 'Linux'},
'browser': {'version': '5.0.307.11', 'name': 'Chrome'}}
>>> s = "Mozilla/5.0 (Linux; U; Android 2.3.5; en-in; HTC_DesireS_S510e Build/GRJ90)
AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"
>>> print(httpagentparser.simple_detect(s))
('Android Linux 2.3.5', 'Safari 4.0')
>>> print(httpagentparser.detect(s))
{'dist': {'version': '2.3.5', 'name': 'Android'},
'os': {'name': 'Linux'},
'browser': {'version': '4.0', 'name': 'Safari'}}
相关文章