Skip to main content

A maintained successor to user-agents: identify devices (phones, tablets) and their capabilities by parsing browser user agent strings.

Project description

user-agents-ng

CI PyPI

A maintained, drop-in successor to user-agents. The original selwin/python-user-agents has had no release since 2020. user-agents-ng picks up where it left off: modern packaging, type hints, and tested support for Python 3.9–3.14. The import stays the same (from user_agents import parse) — just change what you pip install.

user_agents is a Python library that provides an easy way to identify/detect devices like mobile phones, tablets and their capabilities by parsing (browser/HTTP) user agent strings. The goal is to reliably detect whether:

  • User agent is a mobile, tablet or PC based device
  • User agent has touch capabilities (has touch screen)

user_agents relies on the excellent ua-parser to do the actual parsing of the raw user agent string.

Installation

user-agents-ng is hosted on PyPI and can be installed with:

pip install user-agents-ng

Migrating from user-agents

user-agents-ng is a drop-in replacement. Swap the dependency and your code keeps working unchanged:

pip uninstall user-agents
pip install user-agents-ng
from user_agents import parse  # <- unchanged

Usage

Various basic information that can help you identify visitors can be accessed via the browser, device and os attributes. For example:

from user_agents import parse

# iPhone's 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)

# Accessing user agent's browser attributes
user_agent.browser  # returns Browser(family='Mobile Safari', version=(5, 1), version_string='5.1')
user_agent.browser.family  # returns 'Mobile Safari'
user_agent.browser.version  # returns (5, 1)
user_agent.browser.version_string   # returns '5.1'

# Accessing user agent's operating system properties
user_agent.os  # returns OperatingSystem(family='iOS', version=(5, 1), version_string='5.1')
user_agent.os.family  # returns 'iOS'
user_agent.os.version  # returns (5, 1)
user_agent.os.version_string  # returns '5.1'

# Accessing user agent's device properties
user_agent.device  # returns Device(family='iPhone', brand='Apple', model='iPhone')
user_agent.device.family  # returns 'iPhone'
user_agent.device.brand # returns 'Apple'
user_agent.device.model # returns 'iPhone'

# Viewing a pretty string version
str(user_agent) # returns "iPhone / iOS 5.1 / Mobile Safari 5.1"

user_agents also exposes a few other more "sophisticated" attributes that are derived from one or more basic attributes defined above. These attributes should correctly identify popular platforms/devices; pull requests to support smaller ones are always welcome.

Currently these attributes are supported:

  • is_mobile: whether user agent is identified as a mobile phone (iPhone, Android phones, Blackberry, Windows Phone devices etc)
  • is_tablet: whether user agent is identified as a tablet device (iPad, Kindle Fire, Nexus 7 etc)
  • is_pc: whether user agent is identified to be running a traditional "desktop" OS (Windows, OS X, Linux)
  • is_touch_capable: whether user agent has touch capabilities
  • is_bot: whether user agent is a search engine crawler/spider

For example:

from user_agents import parse

# Let's start from an old, non touch Blackberry device
ua_string = 'BlackBerry9700/5.0.0.862 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/331 UNTRUSTED/1.0 3gpp-gba'
user_agent = parse(ua_string)
user_agent.is_mobile # returns True
user_agent.is_tablet # returns False
user_agent.is_touch_capable # returns False
user_agent.is_pc # returns False
user_agent.is_bot # returns False
str(user_agent) # returns "BlackBerry 9700 / BlackBerry OS 5 / BlackBerry 9700"

# Now a Samsung Galaxy S3
ua_string = '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(ua_string)
user_agent.is_mobile # returns True
user_agent.is_tablet # returns False
user_agent.is_touch_capable # returns True
user_agent.is_pc # returns False
user_agent.is_bot # returns False
str(user_agent) # returns "Samsung GT-I9300 / Android 4.0.4 / Android 4.0.4"

# Touch capable Windows 8 device
ua_string = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Touch)'
user_agent = parse(ua_string)
user_agent.is_mobile # returns False
user_agent.is_tablet # returns False
user_agent.is_touch_capable # returns True
user_agent.is_pc # returns True
user_agent.is_bot # returns False
str(user_agent) # returns "PC / Windows 8 / IE 10"

Running Tests

python -m unittest user_agents.tests

Changelog

Version 3.0.0

  • Released as user-agents-ng, a maintained successor to user-agents.
  • Tested support for Python 3.9–3.14; dropped Python 2.
  • Migrated packaging to pyproject.toml; added py.typed and type hints.
  • Replaced Travis CI with GitHub Actions (Linux + Windows).

For the pre-3.0 history, see the original project changelog.

Credits

Originally created by Selwin Ong and the team at Stamps. Maintained as user-agents-ng by Saif Ali. Licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

user_agents_ng-3.0.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

user_agents_ng-3.0.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file user_agents_ng-3.0.0.tar.gz.

File metadata

  • Download URL: user_agents_ng-3.0.0.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for user_agents_ng-3.0.0.tar.gz
Algorithm Hash digest
SHA256 6141d485008f335264760f8126c3a331c50f41feac540a9b7afa2b692ded8df1
MD5 bc60998e529095cfd5fb47594517dd56
BLAKE2b-256 d7a69635cabc2a8b8b184522bff0feb48a82f0da9f5a40e8bd664ce5b7c7787b

See more details on using hashes here.

File details

Details for the file user_agents_ng-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: user_agents_ng-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for user_agents_ng-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8945d8c967a6afcb77f33992b75fbd3d8986e8fabf3d6ad72325e53cf03bc985
MD5 727630a2fd74c207a7fc64949ca4bdab
BLAKE2b-256 1e5b85d09923cd0aea6599b6bbc7969699e1a5b3d832b319ed282e7212f509aa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page