User Agent Parser for Python
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.
Warnings
- 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.
- 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.
- 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.
- 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.
Install
-
pip install ua-parser -
pip install 'ua-parser[regex]' -
pip install 'ua-parser[re2]' -
pip install 'ua-parser[yaml]'
Imports
- parse
from ua_parser import parse
- parse_user_agent
from ua_parser import parse_user_agent
- parse_os
from ua_parser import parse_os
- parse_device
from ua_parser import parse_device
Quickstart
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}")