DESCRIPTION.rst 6.6 KB

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