Parsing HTTP User-Agent string

2019-01-10 22:12发布

What is the best method to parse a User-Agent string in Python to reliably detect

  1. Browser
  2. Browser version
  3. OS

Or perhaps any helper library that does it

7条回答
啃猪蹄的小仙女
2楼-- · 2019-01-10 22:30

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.

查看更多
看我几分像从前
3楼-- · 2019-01-10 22:38

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

查看更多
疯言疯语
4楼-- · 2019-01-10 22:46

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.

查看更多
Anthone
5楼-- · 2019-01-10 22:47

Werkzeug has user-agent parsing built-in.

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

查看更多
手持菜刀,她持情操
6楼-- · 2019-01-10 22:47

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

查看更多
Viruses.
7楼-- · 2019-01-10 22:49

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

查看更多
登录 后发表回答