56

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

Shekhar
  • 7,095
  • 4
  • 40
  • 45
  • More to the point, depending on browser OSes and versions for capability detection is not a good strategy. What problem are you trying to solve? – Dan Davies Brackett May 29 '09 at 19:06
  • 1
    We have a simple ticket reporter form for our web app. And we frequently ask users what's the broswer/OS etc. So we might just detect it beforehand and ask the user to confirm. Well, if thats not so trivial we will just dump the user-agent but still need to ask questions, as user might be using a different browser to submit the ticket. – Shekhar May 29 '09 at 19:12
  • You may wish to just let Python render the form and use Javascript to change the input field values to those of the current OS and what ever else you wish. – NerdyNick May 29 '09 at 19:23

8 Answers8

80

I finally decided to write my own, and I am happy with the outcome. Please feel free to use/modify/send me patches, etc.

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

Usage example:

>>> 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'}}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Shekhar
  • 7,095
  • 4
  • 40
  • 45
  • This doesn't seem to work particularly well for mobile phones. In particular, blackberry isn't detected. (although you got my +1 for writing it!) – Jason Sundram Sep 20 '11 at 04:15
  • 4
    @Jason Sundram If you help by providing agent strings and expected results, blackberry, others can be supported. Check https://github.com/shon/httpagentparser/issues – Shekhar Sep 20 '11 at 13:11
  • I was interested in comparison, but that's a fair point. Retracted. – James Broadhead Apr 14 '12 at 23:00
  • 2
    nice! I've Just wrote a django middleware for my app which prints a message to user if his browser is Internet Explorer < 10 and tested it against a lot of user agent strings... works perfectly! thank you :) – daveoncode Jun 19 '15 at 12:47
  • Is there any way to know the device name i.e desktop, iphone, ipad etc.? – DS_Enthusiast Aug 23 '23 at 21:51
10

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

  • I would recommend UA Parser too. – Dexter Oct 23 '12 at 01:49
  • worth mentioning it's a paid product and a data file that is required by this product and contains user agent definitions is not availkable unless purchased. https://github.com/udger/udger-python/issues/5#issuecomment-739843620 – Maksim Vi. Jul 25 '22 at 20:28
8

Werkzeug has user-agent parsing built-in.

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

Joshua Olson
  • 3,675
  • 3
  • 27
  • 30
  • Interesting. I quickly checked the code. It is really simple, nice code essentially a series of regex to match against agent string. Interesting would be how it performs. One plus point of Werkzeug is it also detects language, while advantage of httpagentparaser is that it also detect os version. – Shekhar Jun 22 '12 at 14:48
  • 1
    The current link is http://werkzeug.pocoo.org/docs/0.14/utils/#module-werkzeug.useragents ; SO rejects that change for being too small. – Nova Jun 07 '18 at 18:05
  • 1
    the user-agent parser is now deprecated. https://werkzeug.palletsprojects.com/en/2.0.x/utils/#module-werkzeug.useragents – Mohammadhzp Sep 05 '21 at 07:29
8

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.

Chris W.
  • 37,583
  • 36
  • 99
  • 136
6

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.

James Broadhead
  • 1,878
  • 1
  • 16
  • 19
3

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

2

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.

NerdyNick
  • 813
  • 1
  • 9
  • 17
1

As this is not about an open source solution, I doubt this will become the first answer. Anyway, when it comes to User-Agent analysis, the de-facto standard is WURFL (now a commercial product.

Here is a reference to the technical docs.

https://docs.scientiamobile.com/documentation/infuze/infuze-python-module-user-guide

In addition to that, WURFL Microservice is available on the major Cloud Providers marketplaces and also supports a Python client:

Luca P.
  • 1,021
  • 8
  • 20