A library to identify devices (phones, tablets) and their capabilities by parsing browser user agent strings.
npx @tessl/cli install tessl/pypi-user-agents@2.2.0A Python library that provides an easy way to identify devices like mobile phones, tablets and their capabilities by parsing browser user agent strings. The library reliably detects whether a user agent represents a mobile, tablet, or PC device and determines touch capabilities.
pip install pyyaml ua-parser user-agentsfrom user_agents import parseAlternative imports:
import user_agents
# Access via module: user_agents.parse(ua_string)
# Access version information
from user_agents import VERSION
# or
import user_agents
version = user_agents.VERSION # (2, 2, 0)from user_agents import parse
# Parse an iPhone user agent string
ua_string = 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3'
user_agent = parse(ua_string)
# Access device type properties
print(user_agent.is_mobile) # True
print(user_agent.is_tablet) # False
print(user_agent.is_touch_capable) # True
print(user_agent.is_pc) # False
print(user_agent.is_bot) # False
# Access device, OS, and browser information
print(user_agent.device.family) # 'iPhone'
print(user_agent.os.family) # 'iOS'
print(user_agent.browser.family) # 'Mobile Safari'
# Get formatted strings
print(str(user_agent)) # "iPhone / iOS 5.1 / Mobile Safari 5.1"
print(user_agent.get_device()) # 'iPhone'
print(user_agent.get_os()) # 'iOS 5.1'
print(user_agent.get_browser()) # 'Mobile Safari 5.1'Parse user agent strings to extract device, operating system, and browser information with device classification capabilities.
def parse(user_agent_string: str) -> UserAgent:
"""
Parse a user agent string and return a UserAgent instance.
Parameters:
- user_agent_string: str, the user agent string to parse
Returns:
UserAgent instance with parsed information
"""The UserAgent class provides comprehensive device type detection and information access.
class UserAgent:
"""
Main user agent analysis class with device detection capabilities.
Attributes:
- ua_string: str, original user agent string
- os: OperatingSystem, operating system information
- browser: Browser, browser information
- device: Device, device information
"""
def __init__(self, user_agent_string: str):
"""Initialize UserAgent with user agent string."""
def __str__(self) -> str:
"""Return formatted string representation: 'Device / OS / Browser'."""
def __unicode__(self) -> str:
"""Return Unicode string representation (Python 2 compatibility)."""
def get_device(self) -> str:
"""Return device name or 'PC' if is_pc is True."""
def get_os(self) -> str:
"""Return formatted OS name with version."""
def get_browser(self) -> str:
"""Return formatted browser name with version."""
@property
def is_mobile(self) -> bool:
"""True if device is identified as a mobile phone."""
@property
def is_tablet(self) -> bool:
"""True if device is identified as a tablet."""
@property
def is_pc(self) -> bool:
"""True if device is identified as a desktop/laptop PC."""
@property
def is_touch_capable(self) -> bool:
"""True if device has touch screen capabilities."""
@property
def is_bot(self) -> bool:
"""True if user agent is a web crawler/spider."""
@property
def is_email_client(self) -> bool:
"""True if user agent is an email client."""
def _is_android_tablet(self) -> bool:
"""
Internal method to determine if Android device is a tablet.
Returns True for Android devices without 'Mobile Safari' in user agent string
(except Firefox Mobile).
"""
def _is_blackberry_touch_capable_device(self) -> bool:
"""
Internal method to determine if BlackBerry device has touch capabilities.
Returns True for BlackBerry Bold Touch series (99XX) and Storm devices (95XX).
"""Structured data containers for parsed user agent components.
class Browser:
"""
Browser information namedtuple.
Attributes:
- family: str, browser family name
- version: tuple, version numbers as tuple
- version_string: str, version as formatted string
"""
class OperatingSystem:
"""
Operating system information namedtuple.
Attributes:
- family: str, OS family name
- version: tuple, version numbers as tuple
- version_string: str, version as formatted string
"""
class Device:
"""
Device information namedtuple.
Attributes:
- family: str, device family name
- brand: str, device brand
- model: str, device model
"""Helper functions for parsing user agent components (primarily for internal use).
def parse_browser(family: str, major=None, minor=None, patch=None, patch_minor=None) -> Browser:
"""Create Browser namedtuple from parsed data."""
def parse_operating_system(family: str, major=None, minor=None, patch=None, patch_minor=None) -> OperatingSystem:
"""Create OperatingSystem namedtuple from parsed data."""
def parse_device(family: str, brand: str, model: str) -> Device:
"""Create Device namedtuple from parsed data."""
def parse_version(major=None, minor=None, patch=None, patch_minor=None) -> tuple:
"""Parse version components into tuple of integers."""
def verify_attribute(attribute):
"""Convert string digits to integers, leave other values unchanged.
Parameters:
- attribute: any, the attribute to verify
Returns:
int if attribute is a digit string, otherwise returns original attribute
"""VERSION: tuple = (2, 2, 0)
# Package version tuple
# Device classification constants
MOBILE_DEVICE_FAMILIES: tuple = (
'iPhone', 'iPod', 'Generic Smartphone', 'Generic Feature Phone',
'PlayStation Vita', 'iOS-Device'
)
TABLET_DEVICE_FAMILIES: tuple = (
'iPad', 'BlackBerry Playbook', 'Blackberry Playbook', 'Kindle',
'Kindle Fire', 'Kindle Fire HD', 'Galaxy Tab', 'Xoom', 'Dell Streak'
)
TOUCH_CAPABLE_DEVICE_FAMILIES: tuple = (
'BlackBerry Playbook', 'Blackberry Playbook', 'Kindle Fire'
)
PC_OS_FAMILIES: tuple = (
'Windows 95', 'Windows 98', 'Solaris'
)
MOBILE_OS_FAMILIES: tuple = (
'Windows Phone', 'Windows Phone OS', 'Symbian OS', 'Bada',
'Windows CE', 'Windows Mobile', 'Maemo'
)
TOUCH_CAPABLE_OS_FAMILIES: tuple = (
'iOS', 'Android', 'Windows Phone', 'Windows CE', 'Windows Mobile',
'Firefox OS', 'MeeGo'
)
MOBILE_BROWSER_FAMILIES: tuple = (
'IE Mobile', 'Opera Mobile', 'Opera Mini', 'Chrome Mobile',
'Chrome Mobile WebView', 'Chrome Mobile iOS'
)
EMAIL_PROGRAM_FAMILIES: set = {
'Outlook', 'Windows Live Mail', 'AirMail', 'Apple Mail', 'Thunderbird',
'Lightning', 'ThunderBrowse', 'The Bat!', 'Lotus Notes', 'IBM Notes',
'Barca', 'MailBar', 'kmail2', 'YahooMobileMail'
}from user_agents import parse
# Mobile phone (Android)
android_ua = 'Mozilla/5.0 (Linux; U; Android 4.0.4; en-gb; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'
user_agent = parse(android_ua)
print(user_agent.is_mobile) # True
print(user_agent.is_touch_capable) # True
print(str(user_agent)) # "Samsung GT-I9300 / Android 4.0.4 / Android 4.0.4"
# Tablet (iPad)
ipad_ua = 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10'
user_agent = parse(ipad_ua)
print(user_agent.is_tablet) # True
print(user_agent.is_touch_capable) # True
print(str(user_agent)) # "iPad / iOS 3.2 / Mobile Safari 4.0.4"
# Desktop PC
pc_ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2'
user_agent = parse(pc_ua)
print(user_agent.is_pc) # True
print(user_agent.is_touch_capable) # False
print(str(user_agent)) # "PC / Mac OS X 10.6.8 / Safari 5.1.7"
# Bot detection
bot_ua = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
user_agent = parse(bot_ua)
print(user_agent.is_bot) # True
print(str(user_agent)) # "Spider / Other / Googlebot 2.1"from user_agents import parse
ua_string = 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3'
user_agent = parse(ua_string)
# Browser details
print(user_agent.browser.family) # 'Mobile Safari'
print(user_agent.browser.version) # (5, 1)
print(user_agent.browser.version_string) # '5.1'
# OS details
print(user_agent.os.family) # 'iOS'
print(user_agent.os.version) # (5, 1)
print(user_agent.os.version_string) # '5.1'
# Device details
print(user_agent.device.family) # 'iPhone'
print(user_agent.device.brand) # 'Apple'
print(user_agent.device.model) # 'iPhone'