Parsing HTTP User-Agent string

我的未来我决定 提交于 2019-11-27 12:01:07

Answering my own question ;)

Finally I decided to go by suggestion#1 i.e. write your own. And I am happy with the outcome. Please feel free to use/modify/send me patch etc.

It's here -> http://pypi.python.org/pypi/httpagentparser

UASparser for Python by Hicro Kee. Auto updated datafile and cache from remote server with version checking.

Werkzeug has user-agent parsing built-in.

New link (Jun 2018) http://werkzeug.pocoo.org/docs/0.14/utils/#module-werkzeug.useragents

Having run these suggestions against the full corpus of Firefox User Agents, I've found that the version-number parsing for comparison is quite poor.

If that's what you need, I suggest that you take a look at UAparser, which used to be part of the browserscope project. Documentation here.

The other responses to this question are rather old now. I believe the new standard in Browser User Agent parsing is Browserscope's user agent parser.

Also conveniently available with the exact same matching patterns in many other languages. Someday you might want to also parse some UA strings in JavaScript and you don't need to worry about inconsistent parsing.

Th Browser Cap Parser should work. It may be a bit slow though..

However if you wish to parse all this on the Python side you can use the XML/INI files provided at http://browsers.garykeith.com/downloads.asp to do lookups on the user agent. This is the same file that is used in php's get_browser() function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!