|
| 1 | +Python User Agents |
| 2 | +================== |
| 3 | + |
| 4 | +`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: |
| 5 | + |
| 6 | +- User agent is a mobile, tablet or PC based device |
| 7 | +- User agent has touch capabilities (has touch screen) |
| 8 | + |
| 9 | +`user_agents` relies on the excellent [ua-parser](https://github.com/ua-parser/uap-python) to do the actual parsing of the raw user agent string. |
| 10 | + |
| 11 | +Installation |
| 12 | +------------ |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +`user-agents` is hosted on [PyPI](http://pypi.python.org/pypi/user-agents/) and can be installed as such: |
| 17 | + |
| 18 | + pip install pyyaml ua-parser user-agents |
| 19 | + |
| 20 | +Alternatively, you can also get the latest source code from [Github](https://github.com/selwin/python-user-agents) and install it manually. |
| 21 | + |
| 22 | +Usage |
| 23 | +----- |
| 24 | + |
| 25 | +Various basic information that can help you identify visitors can be accessed `browser`, `device` and `os` attributes. For example: |
| 26 | + |
| 27 | +```python |
| 28 | +from user_agents import parse |
| 29 | + |
| 30 | +# iPhone's user agent string |
| 31 | +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' |
| 32 | +user_agent = parse(ua_string) |
| 33 | + |
| 34 | +# Accessing user agent's browser attributes |
| 35 | +user_agent.browser # returns Browser(family=u'Mobile Safari', version=(5, 1), version_string='5.1') |
| 36 | +user_agent.browser.family # returns 'Mobile Safari' |
| 37 | +user_agent.browser.version # returns (5, 1) |
| 38 | +user_agent.browser.version_string # returns '5.1' |
| 39 | + |
| 40 | +# Accessing user agent's operating system properties |
| 41 | +user_agent.os # returns OperatingSystem(family=u'iOS', version=(5, 1), version_string='5.1') |
| 42 | +user_agent.os.family # returns 'iOS' |
| 43 | +user_agent.os.version # returns (5, 1) |
| 44 | +user_agent.os.version_string # returns '5.1' |
| 45 | + |
| 46 | +# Accessing user agent's device properties |
| 47 | +user_agent.device # returns Device(family=u'iPhone', brand=u'Apple', model=u'iPhone') |
| 48 | +user_agent.device.family # returns 'iPhone' |
| 49 | +user_agent.device.brand # returns 'Apple' |
| 50 | +user_agent.device.model # returns 'iPhone' |
| 51 | + |
| 52 | +# Viewing a pretty string version |
| 53 | +str(user_agent) # returns "iPhone / iOS 5.1 / Mobile Safari 5.1" |
| 54 | +``` |
| 55 | + |
| 56 | +`user_agents` also 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. |
| 57 | + |
| 58 | +Currently these attributes are supported: |
| 59 | + |
| 60 | +- `is_mobile`: whether user agent is identified as a mobile phone (iPhone, Android phones, Blackberry, Windows Phone devices etc) |
| 61 | +- `is_tablet`: whether user agent is identified as a tablet device (iPad, Kindle Fire, Nexus 7 etc) |
| 62 | +- `is_pc`: whether user agent is identified to be running a traditional "desktop" OS (Windows, OS X, Linux) |
| 63 | +- `is_touch_capable`: whether user agent has touch capabilities |
| 64 | +- `is_bot`: whether user agent is a search engine crawler/spider |
| 65 | + |
| 66 | +For example: |
| 67 | + |
| 68 | +```python |
| 69 | +from user_agents import parse |
| 70 | + |
| 71 | +# Let's start from an old, non touch Blackberry device |
| 72 | +ua_string = 'BlackBerry9700/5.0.0.862 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/331 UNTRUSTED/1.0 3gpp-gba' |
| 73 | +user_agent = parse(ua_string) |
| 74 | +user_agent.is_mobile # returns True |
| 75 | +user_agent.is_tablet # returns False |
| 76 | +user_agent.is_touch_capable # returns False |
| 77 | +user_agent.is_pc # returns False |
| 78 | +user_agent.is_bot # returns False |
| 79 | +str(user_agent) # returns "BlackBerry 9700 / BlackBerry OS 5 / BlackBerry 9700" |
| 80 | + |
| 81 | +# Now a Samsung Galaxy S3 |
| 82 | +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' |
| 83 | +user_agent = parse(ua_string) |
| 84 | +user_agent.is_mobile # returns True |
| 85 | +user_agent.is_tablet # returns False |
| 86 | +user_agent.is_touch_capable # returns True |
| 87 | +user_agent.is_pc # returns False |
| 88 | +user_agent.is_bot # returns False |
| 89 | +str(user_agent) # returns "Samsung GT-I9300 / Android 4.0.4 / Android 4.0.4" |
| 90 | + |
| 91 | +# iPad's user agent string |
| 92 | +ua_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' |
| 93 | +user_agent = parse(ua_string) |
| 94 | +user_agent.is_mobile # returns False |
| 95 | +user_agent.is_tablet # returns True |
| 96 | +user_agent.is_touch_capable # returns True |
| 97 | +user_agent.is_pc # returns False |
| 98 | +user_agent.is_bot # returns False |
| 99 | +str(user_agent) # returns "iPad / iOS 3.2 / Mobile Safari 4.0.4" |
| 100 | + |
| 101 | +# Kindle Fire's user agent string |
| 102 | +ua_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' |
| 103 | +user_agent = parse(ua_string) |
| 104 | +user_agent.is_mobile # returns False |
| 105 | +user_agent.is_tablet # returns True |
| 106 | +user_agent.is_touch_capable # returns True |
| 107 | +user_agent.is_pc # returns False |
| 108 | +user_agent.is_bot # returns False |
| 109 | +str(user_agent) # returns "Kindle / Android / Amazon Silk 1.1.0-80" |
| 110 | + |
| 111 | +# Touch capable Windows 8 device |
| 112 | +ua_string = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Touch)' |
| 113 | +user_agent = parse(ua_string) |
| 114 | +user_agent.is_mobile # returns False |
| 115 | +user_agent.is_tablet # returns False |
| 116 | +user_agent.is_touch_capable # returns True |
| 117 | +user_agent.is_pc # returns True |
| 118 | +user_agent.is_bot # returns False |
| 119 | +str(user_agent) # returns "PC / Windows 8 / IE 10" |
| 120 | +``` |
| 121 | + |
| 122 | +Running Tests |
| 123 | +------------- |
| 124 | + |
| 125 | + python -m unittest discover |
| 126 | + |
| 127 | +Changelog |
| 128 | +--------- |
| 129 | + |
| 130 | +### Version 2.1 (2020-02-08) |
| 131 | + |
| 132 | +- `python-user-agents` now require `ua-parser>=0.9.0`. Thanks @jnozsc! |
| 133 | +- Properly detect Chrome Mobile browser families. Thanks @jnozsc! |
| 134 | + |
| 135 | +### Version 2.0 (2019-04-07) |
| 136 | + |
| 137 | +- `python-user-agents` now require `ua-parser>=0.8.0`. Thanks @IMDagger! |
| 138 | + |
| 139 | +### Version 1.1 |
| 140 | + |
| 141 | +- Fixes packaging issue |
| 142 | + |
| 143 | +### Version 1.0 |
| 144 | + |
| 145 | +- Adds compatibility with `ua-parser` 0.4.0 |
| 146 | +- Access to more device information in `user_agent.device.brand` and `user_agent.device.model` |
| 147 | + |
| 148 | +### Version 0.3.2 |
| 149 | + |
| 150 | +- Better mobile detection |
| 151 | +- Better PC detection |
| 152 | + |
| 153 | +### Version 0.3.1 |
| 154 | + |
| 155 | +- user\_agent.is\_mobile returns True when mobile spider is detected |
| 156 | + |
| 157 | +### Version 0.3.0 |
| 158 | + |
| 159 | +- Added **str**/**unicode** methods for convenience of pretty string |
| 160 | + |
| 161 | +### Version 0.2.0 |
| 162 | + |
| 163 | +- Fixed errors when running against newer versions if ua-parser |
| 164 | +- Support for Python 3 |
| 165 | + |
| 166 | +### Version 0.1.1 |
| 167 | + |
| 168 | +- Added `is_bot` property |
| 169 | +- Symbian OS devices are now detected as a mobile device |
| 170 | + |
| 171 | +### Version 0.1 |
| 172 | + |
| 173 | +- Initial release |
| 174 | + |
| 175 | +Developed by the cool guys at [Stamps](http://stamps.co.id). |
0 commit comments