Parsing HTTP User-Agent string

PythonHttpHttp HeadersUser Agent

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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'}}

Solution 2 - Python

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

Solution 3 - Python

Werkzeug has user-agent parsing built-in.

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

Solution 4 - Python

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.

Solution 5 - Python

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.

Solution 6 - Python

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

Solution 7 - Python

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.

Solution 8 - Python

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:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShekharView Question on Stackoverflow
Solution 1 - PythonShekharView Answer on Stackoverflow
Solution 2 - PythonJarView Answer on Stackoverflow
Solution 3 - PythonJoshua OlsonView Answer on Stackoverflow
Solution 4 - PythonChris W.View Answer on Stackoverflow
Solution 5 - PythonJames BroadheadView Answer on Stackoverflow
Solution 6 - PythonChirayu PatelView Answer on Stackoverflow
Solution 7 - PythonNerdyNickView Answer on Stackoverflow
Solution 8 - PythonLuca PassaniView Answer on Stackoverflow