Package: user-agents
Python User Agentsuser_agentsis 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 deviceUser agent has touch capabilities (has touch screen)user_agentsrelies on the excellentua-parserto do the actual parsing of the raw user agent string.Installationuser-agentsis hosted onPyPIand can be installed as such:pip install pyyaml ua-parser user-agentsAlternatively, you can also get the latest source code fromGithuband install it manually.UsageVarious basic information that can help you identify visitors can be accessedbrowser,deviceandosattributes. For example:fromuser_agentsimportparse# iPhone's user agent stringua_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 attributesuser_agent.browser# returns Browser(family=u'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 propertiesuser_agent.os# returns OperatingSystem(family=u'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 propertiesuser_agent.device# returns Device(family=u'iPhone', brand=u'Apple', model=u'iPhone')user_agent.device.family# returns 'iPhone'user_agent.device.brand# returns 'Apple'user_agent.device.model# returns 'iPhone'# Viewing a pretty string versionstr(user_agent)# returns "iPhone / iOS 5.1 / Mobile Safari 5.1"user_agentsalso expose a few other more "sophisticated" attributes that are derived from one or more basic attributes defined above. As for now, 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 capabilitiesis_bot: whether user agent is a search engine crawler/spiderFor example:fromuser_agentsimportparse# Let's start from an old, non touch Blackberry deviceua_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 Trueuser_agent.is_tablet# returns Falseuser_agent.is_touch_capable# returns Falseuser_agent.is_pc# returns Falseuser_agent.is_bot# returns Falsestr(user_agent)# returns "BlackBerry 9700 / BlackBerry OS 5 / BlackBerry 9700"# Now a Samsung Galaxy S3ua_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 Trueuser_agent.is_tablet# returns Falseuser_agent.is_touch_capable# returns Trueuser_agent.is_pc# returns Falseuser_agent.is_bot# returns Falsestr(user_agent)# returns "Samsung GT-I9300 / Android 4.0.4 / Android 4.0.4"# iPad's user agent stringua_string='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(ua_string)user_agent.is_mobile# returns Falseuser_agent.is_tablet# returns Trueuser_agent.is_touch_capable# returns Trueuser_agent.is_pc# returns Falseuser_agent.is_bot# returns Falsestr(user_agent)# returns "iPad / iOS 3.2 / Mobile Safari 4.0.4"# Kindle Fire's user agent stringua_string='Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.1.0-80) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=true'user_agent=parse(ua_string)user_agent.is_mobile# returns Falseuser_agent.is_tablet# returns Trueuser_agent.is_touch_capable# returns Trueuser_agent.is_pc# returns Falseuser_agent.is_bot# returns Falsestr(user_agent)# returns "Kindle / Android / Amazon Silk 1.1.0-80"# Touch capable Windows 8 deviceua_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 Falseuser_agent.is_tablet# returns Falseuser_agent.is_touch_capable# returns Trueuser_agent.is_pc# returns Trueuser_agent.is_bot# returns Falsestr(user_agent)# returns "PC / Windows 8 / IE 10"Running Testspython -m unittest discoverChangelogVersion 2.2.0 (2020-08-23)ua-parser>= 0.10.0 is required. Thanks @jnozsc!Addedget_device(),get_os()andget_browser()instance methods
toUserAgent. Thanks @rodrigondec!Version 2.1 (2020-02-08)python-user-agentsnow requireua-parser>=0.9.0. Thanks @jnozsc!Properly detect Chrome Mobile browser families. Thanks @jnozsc!Version 2.0 (2019-04-07)python-user-agentsnow requireua-parser>=0.8.0. Thanks @IMDagger!Version 1.1Fixes packaging issueVersion 1.0Adds compatibility withua-parser0.4.0Access to more device information inuser_agent.device.brandanduser_agent.device.modelVersion 0.3.2Better mobile detectionBetter PC detectionVersion 0.3.1user_agent.is_mobile returns True when mobile spider is detectedVersion 0.3.0Addedstr/unicodemethods for convenience of pretty stringVersion 0.2.0Fixed errors when running against newer versions if ua-parserSupport for Python 3Version 0.1.1Addedis_botpropertySymbian OS devices are now detected as a mobile deviceVersion 0.1Initial releaseDeveloped by the cool guys atStamps.