Skip to main content

Python 3 library to parse User-Agent strings and detect browser, OS and device details

Project description

PyPI Downloads Python Versions License Codecov CI Wheel Implementation Code style: black Linter: ruff Docs Open Issues

⭐ If you find this project useful, please star it on GitHub!

Python User Agent Parser

user_agent_parser is a Python 3 library that provides an easy way to identify/detect devices from user agent string

  • User agent is a mobile or computer
  • User agent Browser name and versions
  • User agent Device name

user_agent_parser hosted on PyPI and can be installed as such:

pip install user-agent-parser

Alternatively, you can also get the latest source code from Github and install it manually.

🚀 Quick Start

Optimized Usage (Recommended)

For best performance, use the cached parse() function - up to 112x faster than the original API:

from user_agent_parser import parse

iphone_ua_str = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/92.0.4515.90 Mobile/15E148 Safari/604.1"

# Fast cached parsing - recommended for production
browser, browser_version, os, os_version, device_type, device_name, device_host = parse(iphone_ua_str)

print(f"Browser: {browser} {browser_version}")  # Chrome 92.0.4515.90
print(f"OS: {os} {os_version}")                # iOS 13_6
print(f"Device: {device_name}")                # iPhone
print(f"Type: {device_type}")                  # Mobile

Traditional Usage

The original Parser class is still fully supported:

from user_agent_parser import Parser

iphone_ua_str = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/92.0.4515.90 Mobile/15E148 Safari/604.1"
parser = Parser(iphone_ua_str)

# Calling parser
browser, browser_version, os, os_version, device_type, device_name, device_host = parser()

# Or access properties directly
print(parser.browser)        # Chrome
print(parser.device_name)    # iPhone
print(parser.os)             # iOS

🚀 Performance & Advanced Features

Performance Benchmarks

Based on comprehensive testing with 25+ modern user agents (2024-2025):

Feature Requests/Second Accuracy Memory Usage
Legacy Parser 6,324 RPS 80% Baseline
Cached Parser 219,420 RPS 80% Low
Advanced Engine 33,380 RPS 95% 5.2 MB
Batch Processing 88,258 RPS 92% Optimized

🏆 14x Performance Improvement with advanced batch processing!

🧠 Advanced Features

ML-Inspired Pattern Matching

from user_agent_parser import analyze, DeviceCategory

# Advanced analysis with confidence scoring
result = analyze(user_agent, include_security=True, include_capabilities=True)

print(f"Device: {result.device_name}")
print(f"Category: {result.device_category.value}")  # smartphone, tablet, desktop, etc.
print(f"Confidence: {result.confidence_score:.2f}")
print(f"Detection: {result.detection_confidence.value}")  # high, medium, low

# Browser capabilities
print(f"WebGL Support: {result.capabilities.webgl_support}")
print(f"WebRTC Support: {result.capabilities.webrtc_support}")
print(f"Touch Support: {result.capabilities.touch_support}")

# Security insights
print(f"Privacy Mode: {result.security.privacy_mode}")
print(f"Ad Blocker: {result.security.has_adblocker}")

Batch Analytics & Processing

from user_agent_parser import batch_analyze, generate_analytics

# Process thousands of user agents in parallel
user_agents = ["Mozilla/5.0...", "Chrome/120.0...", ...]
results = batch_analyze(user_agents, max_workers=4)

# Generate comprehensive analytics
report = generate_analytics(user_agents, export_format='json')
print(f"Mobile traffic: {report.mobile_vs_desktop['mobile']} requests")
print(f"Top browser: {max(report.browser_distribution, key=report.browser_distribution.get)}")
print(f"Bot detection: {report.bot_detection_rate:.1%}")

Modern Device Support (2024-2025)

  • iPhone 15 Series with A17 Pro chip detection
  • Samsung Galaxy S24 Ultra with AI features
  • Google Pixel 8 Pro with Tensor G3
  • Gaming Consoles: PS5 Pro, Xbox Series X/S
  • VR/AR: Meta Quest 3, Apple Vision Pro
  • Smart Watches: Apple Watch Series 9, Galaxy Watch 6

Browser Capabilities Detection

  • WebGPU, WebAssembly, WebXR support detection
  • PWA capabilities (Service Workers, Push Notifications)
  • Security features (COOP, COEP, Trusted Types)
  • Performance APIs (Background Sync, Payment Request)

Comprehensive Analytics

# Export detailed reports
report = generate_analytics(user_agents, export_format='csv', filename='traffic_analysis')

# Key insights provided:
# • Browser/OS distribution
# • Mobile vs Desktop breakdown  
# • Device brand analysis
# • Security/Privacy trends
# • Performance metrics
# • Bot detection rates

Perfect for high-throughput applications, web analytics, security analysis, and modern web development!

Running Tests

poetry run pytest

Changelog

See CHANGELOG.md for full version history.

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_agent_parser-0.2.0.tar.gz (34.9 kB view details)

Uploaded Source

Built Distribution

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

user_agent_parser-0.2.0-py3-none-any.whl (35.3 kB view details)

Uploaded Python 3

File details

Details for the file user_agent_parser-0.2.0.tar.gz.

File metadata

  • Download URL: user_agent_parser-0.2.0.tar.gz
  • Upload date:
  • Size: 34.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.2 Darwin/25.3.0

File hashes

Hashes for user_agent_parser-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2683c934e143488d37421f12eedb55147a10e7337e0c19ee9632056ca9afe70a
MD5 6bafb6395950eea4ee2b7b003186dbcb
BLAKE2b-256 268b7fb7b87af188de1f914aa4010dc3a59d60dcf2bfbe38e42ada9754d39bf3

See more details on using hashes here.

File details

Details for the file user_agent_parser-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: user_agent_parser-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 35.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.2 Darwin/25.3.0

File hashes

Hashes for user_agent_parser-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 214fa0ef85726ce7af795fe66d88e9660104c4d1060c9460f6bfa7e7305ee5c8
MD5 15c73894f808546656b398e29511b471
BLAKE2b-256 7fef17b2c860feef5a5ef4a7318fe2e8df57606acda8a3facb3b6acc37ffc63e

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