User Agent Parser for Python

raw JSON →
1.0.1 verified Tue May 12 auth: no python install: verified

ua-parser is a Python port of Browserscope's user agent parser. It provides functionality to detect details like browser, operating system, CPU, and device type/model from a given User-Agent string. The library is actively maintained, with its latest version being 1.0.1, and releases are made to keep up with user agent string changes, although core regex updates can sometimes require specific attention.

pip install ua-parser
error ModuleNotFoundError: No module named 'ua_parser'
cause The 'ua-parser' library is not installed in the current Python environment.
fix
pip install ua-parser
error AttributeError: module 'ua_parser' has no attribute 'Parse'
cause The 'Parse' function is located within the 'user_agent_parser' submodule, not directly under the top-level 'ua_parser' module, when 'import ua_parser' is used.
fix
from ua_parser import user_agent_parser parsed_ua = user_agent_parser.Parse(ua_string)
error ImportError: cannot import name 'Parse' from 'ua_parser'
cause The 'Parse' function is not directly exposed by the top-level 'ua_parser' package; it must be imported from its submodule.
fix
from ua_parser import user_agent_parser parsed_ua = user_agent_parser.Parse(ua_string)
gotcha Without installing optional dependencies like 'regex' or 'google-re2', the library relies on a pure Python resolver which is significantly slower, especially on non-CPython runtimes. This can lead to unexpected performance bottlenecks.
fix Install with recommended optional dependencies: `pip install 'ua-parser[regex]'` for optimal performance.
breaking Google Chrome is progressively 'freezing' User-Agent strings and moving towards User-Agent Client Hints. Libraries relying solely on traditional User-Agent strings (like `ua-parser` by default) may experience declining accuracy for modern Chrome browsers over time as less information is available in the frozen string.
fix Be aware of this industry shift. Consider supplementing `ua-parser` with a solution that handles User-Agent Client Hints if high accuracy for Chrome is critical. The `ua-parser` library itself does not inherently parse client hints.
gotcha The accuracy of user agent parsing relies on up-to-date regex definitions. While `ua-parser` is maintained, keeping its internal regexes current with the latest devices and browsers might require manual updates or more frequent dependency updates compared to solutions that offer automatic regex updates.
fix Regularly update the `ua-parser` library to benefit from the latest regex definitions. For critical applications, monitor parsing results and consider how the underlying `uap-core` definitions are updated.
deprecated In versions prior to 1.0 (e.g., 0.x), the parser could be re-created on every access if not explicitly handled, negating the benefits of faster regex engines and caching. This was a common performance footgun.
fix Upgrade to `ua-parser` version 1.0 or newer. If staying on 0.x, ensure the global parser is explicitly set or cached to avoid re-creation overhead.
gotcha Installing the `google-re2` optional dependency (part of `ua-parser[re2]`) requires a C++ compiler (like `g++`) and related development headers to be present in the build environment. Minimal Python images (e.g., `python:*-slim`) often lack these, leading to build failures for `google-re2`.
fix To use the `re2` engine, ensure your environment has a C++ compiler (e.g., install `build-essential` or `g++` package) and Python development headers before attempting to install `ua-parser[re2]`. Alternatively, consider using `ua-parser[regex]` which relies on `ua-parser-rs` and may have fewer system-level build dependencies.
pip install 'ua-parser[regex]'
pip install 'ua-parser[re2]'
pip install 'ua-parser[yaml]'
python os / libc variant status wheel install import disk
3.10 alpine (musl) re2 build_error - - - -
3.10 alpine (musl) re2 - - - -
3.10 alpine (musl) regex build_error - - - -
3.10 alpine (musl) regex - - - -
3.10 alpine (musl) yaml wheel - 0.15s 20.9M
3.10 alpine (musl) yaml - - 0.17s 20.9M
3.10 alpine (musl) ua-parser wheel - 0.06s 18.7M
3.10 alpine (musl) ua-parser - - 0.07s 18.7M
3.10 slim (glibc) re2 wheel 1.9s 0.04s 21M
3.10 slim (glibc) re2 - - 0.04s 21M
3.10 slim (glibc) regex wheel 1.7s 0.04s 22M
3.10 slim (glibc) regex - - 0.04s 22M
3.10 slim (glibc) yaml wheel 1.7s 0.09s 22M
3.10 slim (glibc) yaml - - 0.10s 22M
3.10 slim (glibc) ua-parser wheel 1.5s 0.04s 19M
3.10 slim (glibc) ua-parser - - 0.04s 19M
3.11 alpine (musl) re2 build_error - - - -
3.11 alpine (musl) re2 - - - -
3.11 alpine (musl) regex build_error - - - -
3.11 alpine (musl) regex - - - -
3.11 alpine (musl) yaml wheel - 0.33s 23.1M
3.11 alpine (musl) yaml - - 0.40s 23.1M
3.11 alpine (musl) ua-parser wheel - 0.13s 20.8M
3.11 alpine (musl) ua-parser - - 0.12s 20.7M
3.11 slim (glibc) re2 wheel 1.9s 0.10s 23M
3.11 slim (glibc) re2 - - 0.09s 23M
3.11 slim (glibc) regex wheel 1.8s 0.09s 24M
3.11 slim (glibc) regex - - 0.09s 24M
3.11 slim (glibc) yaml wheel 1.8s 0.30s 24M
3.11 slim (glibc) yaml - - 0.29s 24M
3.11 slim (glibc) ua-parser wheel 1.7s 0.09s 21M
3.11 slim (glibc) ua-parser - - 0.09s 21M
3.12 alpine (musl) re2 build_error - - - -
3.12 alpine (musl) re2 - - - -
3.12 alpine (musl) regex build_error - - - -
3.12 alpine (musl) regex - - - -
3.12 alpine (musl) yaml wheel - 0.24s 15.0M
3.12 alpine (musl) yaml - - 0.26s 15.0M
3.12 alpine (musl) ua-parser wheel - 0.09s 12.6M
3.12 alpine (musl) ua-parser - - 0.10s 12.6M
3.12 slim (glibc) re2 wheel 1.7s 0.10s 15M
3.12 slim (glibc) re2 - - 0.10s 15M
3.12 slim (glibc) regex wheel 1.6s 0.09s 16M
3.12 slim (glibc) regex - - 0.09s 16M
3.12 slim (glibc) yaml wheel 1.6s 0.26s 16M
3.12 slim (glibc) yaml - - 0.29s 16M
3.12 slim (glibc) ua-parser wheel 1.5s 0.09s 13M
3.12 slim (glibc) ua-parser - - 0.10s 13M
3.13 alpine (musl) re2 build_error - - - -
3.13 alpine (musl) re2 - - - -
3.13 alpine (musl) regex build_error - - - -
3.13 alpine (musl) regex - - - -
3.13 alpine (musl) yaml wheel - 0.23s 14.7M
3.13 alpine (musl) yaml - - 0.24s 14.6M
3.13 alpine (musl) ua-parser wheel - 0.08s 12.3M
3.13 alpine (musl) ua-parser - - 0.09s 12.2M
3.13 slim (glibc) re2 wheel 1.8s 0.08s 14M
3.13 slim (glibc) re2 - - 0.09s 14M
3.13 slim (glibc) regex wheel 1.6s 0.08s 16M
3.13 slim (glibc) regex - - 0.08s 16M
3.13 slim (glibc) yaml wheel 1.6s 0.24s 16M
3.13 slim (glibc) yaml - - 0.28s 16M
3.13 slim (glibc) ua-parser wheel 1.5s 0.08s 13M
3.13 slim (glibc) ua-parser - - 0.08s 13M
3.9 alpine (musl) re2 build_error - - - -
3.9 alpine (musl) re2 - - - -
3.9 alpine (musl) regex build_error - - - -
3.9 alpine (musl) regex - - - -
3.9 alpine (musl) yaml wheel - 0.13s 20.3M
3.9 alpine (musl) yaml - - 0.16s 20.3M
3.9 alpine (musl) ua-parser wheel - 0.06s 18.2M
3.9 alpine (musl) ua-parser - - 0.07s 18.2M
3.9 slim (glibc) re2 build_error - 3.1s - -
3.9 slim (glibc) re2 - - - -
3.9 slim (glibc) regex wheel 2.1s 0.05s 48M
3.9 slim (glibc) regex - - 0.05s 48M
3.9 slim (glibc) yaml wheel 2.0s 0.12s 21M
3.9 slim (glibc) yaml - - 0.13s 21M
3.9 slim (glibc) ua-parser wheel 1.9s 0.06s 19M
3.9 slim (glibc) ua-parser - - 0.05s 19M

This quickstart demonstrates how to use `ua-parser` to parse common User-Agent strings and extract information about the browser, operating system, and device. The `parse` function returns a Result object containing detailed attributes.

from ua_parser import parse

# Example User-Agent strings
ua_string_desktop = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'
ua_string_mobile = 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Mobile/15E148 Safari/604.1'

# Parse a desktop User-Agent string
parsed_ua_desktop = parse(ua_string_desktop)
print(f"Desktop UA: {ua_string_desktop}")
print(f"  Browser: {parsed_ua_desktop.user_agent.family} {parsed_ua_desktop.user_agent.major}.{parsed_ua_desktop.user_agent.minor}")
print(f"  OS: {parsed_ua_desktop.os.family} {parsed_ua_desktop.os.major}.{parsed_ua_desktop.os.minor}")
print(f"  Device: {parsed_ua_desktop.device.family}\n")

# Parse a mobile User-Agent string
parsed_ua_mobile = parse(ua_string_mobile)
print(f"Mobile UA: {ua_string_mobile}")
print(f"  Browser: {parsed_ua_mobile.user_agent.family} {parsed_ua_mobile.user_agent.major}.{parsed_ua_mobile.user_agent.minor}")
print(f"  OS: {parsed_ua_mobile.os.family} {parsed_ua_mobile.os.major}.{parsed_ua_mobile.os.minor}")
print(f"  Device: {parsed_ua_mobile.device.family}")