Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Python implementation of ua-parser

License

NotificationsYou must be signed in to change notification settings

ua-parser/uap-python

Repository files navigation

Official python implementation of theUser Agent StringParser project.

Build Status

CI on the master branchDocumentation Status

Installing

Addua-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.9 and newer, recent pypy (supporting3.10), and GraalPy 24.

Note

The[regex] feature isstrongly recommended, the Pure python(no feature) issignificantly slower, especially on non-cpythonruntimes, though it is the most memory efficient.

Seebuiltin resolvers for more explanation of the tradeoffsbetween the different options.

Quick Start

Retrieve all data on a user-agent string

>>>fromua_parserimportparse>>>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, +ELLIPSISResult(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;IntelMacOS...

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

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

Extract only browser data from user-agent string

>>>fromua_parserimportparse_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 returnsNone:

>>> parse_user_agent("")

Extract OS information from user-agent string

>>>fromua_parserimportparse_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

>>>fromua_parserimportparse_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? Seethe upgrade guide.


[8]ページ先頭

©2009-2025 Movatter.jp