← Winnitude! Search  ·  Reader Mode (803 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 67 LINKS

GitHub - ua-parser/uap-python: Python implementation of ua-parser

Repository navigation

uap-python
Public

About

Python implementation of ua-parser

Topics

python3 user-agent user-agent-parser user-agent-parsing

Resources

Readme Apache-2.0 license Activity Custom properties

Stars

651 stars

Watchers

25 watching

Forks

157 forks Report repository
Branches

Tags
Go to Branches page Go to Tags page

Latest commit

 

History


312 Commits
View commit history for this file.

Folders and files

Name Name Last commit message Last commit date
.github .github    
doc doc    
samples samples    
scripts scripts    
src/ ua_parser src/ ua_parser    
tests tests    
ua-parser-builtins ua-parser-builtins    
ua-parser-rs ua-parser-rs    
uap-core @ 383604d uap-core @ 383604d    
.gitignore .gitignore    
.gitmodules .gitmodules    
.readthedocs.yaml .readthedocs.yaml    
LICENSE LICENSE    
MANIFEST.in MANIFEST.in    
README.rst README.rst    
pyproject.toml pyproject.toml    
tox.ini tox.ini    

Repository files navigation

uap-python

Official python implementation of the User Agent String Parser project.

Build Status

Installing

Add ua-parser[regex] to your project's dependencies, or run

$ pip install 'ua-parser[regex]'

to install in the current environment.

ua-parser supports CPython 3.10 and newer, recent pypy (supporting 3.10), and GraalPy 25.

Note

The [regex] feature is strongly recommended, the Pure python (no feature) is significantly slower, especially on non-cpython runtimes, though it is the most memory efficient.

See builtin resolvers for more explanation of the tradeoffs between the different options.

Quick Start

Retrieve all data on a user-agent string

>>> from ua_parser import parse
>>> ua_string = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36'
>>> parse(ua_string) # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
Result(user_agent=UserAgent(family='Chrome',
                            major='41',
                            minor='0',
                            patch='2272',
                            patch_minor='104'),
       os=OS(family='Mac OS X',
             major='10',
             minor='9',
             patch='4',
             patch_minor=None),
       device=Device(family='Mac',
                     brand='Apple',
                     model='Mac'),
       string='Mozilla/5.0 (Macintosh; Intel Mac OS...

Any datum not found in the user agent string is set to None:

>>> parse("")
Result(user_agent=None, os=None, device=None, string='')

Extract only browser data from user-agent string

>>> from ua_parser import parse_user_agent
>>> ua_string = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36'
>>> parse_user_agent(ua_string)
UserAgent(family='Chrome', major='41', minor='0', patch='2272', patch_minor='104')

For specific domains, a match failure just returns None:

>>> parse_user_agent("")

Extract OS information from user-agent string

>>> from ua_parser import parse_os
>>> ua_string = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36'
>>> parse_os(ua_string)
OS(family='Mac OS X', major='10', minor='9', patch='4', patch_minor=None)

Extract device information from user-agent string

>>> from ua_parser import parse_device
>>> ua_string = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36'
>>> parse_device(ua_string)
Device(family='Mac', brand='Apple', model='Mac')

Upgrading

Upgrading from 0.x? See the upgrade guide .

About

Python implementation of ua-parser

Topics

python3 user-agent user-agent-parser user-agent-parsing

Resources

Readme Apache-2.0 license Activity Custom properties

Stars

651 stars

Watchers

25 watching

Forks

157 forks Report repository

Releases

Used by

Contributors

Languages