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