A Python library that provides an easy way to identify devices like mobile phones, tablets and their capabilities by parsing (browser) user agent strings.

Overview

Python User Agents

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:

  • User agent is a mobile, tablet or PC based device
  • User agent has touch capabilities (has touch screen)

user_agents relies on the excellent ua-parser to do the actual parsing of the raw user agent string.

Installation

Build status

user-agents is hosted on PyPI and can be installed as such:

pip install pyyaml ua-parser user-agents

Alternatively, you can also get the latest source code from Github and install it manually.

Usage

Various basic information that can help you identify visitors can be accessed browser, device and os attributes. For example:

from user_agents import parse

# iPhone's user agent string
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'
user_agent = parse(ua_string)

# Accessing user agent's browser attributes
user_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 properties
user_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 properties
user_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 version
str(user_agent) # returns "iPhone / iOS 5.1 / Mobile Safari 5.1"

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.

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 capabilities
  • is_bot: whether user agent is a search engine crawler/spider

For example:

from user_agents import parse

# Let's start from an old, non touch Blackberry device
ua_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 True
user_agent.is_tablet # returns False
user_agent.is_touch_capable # returns False
user_agent.is_pc # returns False
user_agent.is_bot # returns False
str(user_agent) # returns "BlackBerry 9700 / BlackBerry OS 5 / BlackBerry 9700"

# Now a Samsung Galaxy S3
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'
user_agent = parse(ua_string)
user_agent.is_mobile # returns True
user_agent.is_tablet # returns False
user_agent.is_touch_capable # returns True
user_agent.is_pc # returns False
user_agent.is_bot # returns False
str(user_agent) # returns "Samsung GT-I9300 / Android 4.0.4 / Android 4.0.4"

# iPad's user agent string
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'
user_agent = parse(ua_string)
user_agent.is_mobile # returns False
user_agent.is_tablet # returns True
user_agent.is_touch_capable # returns True
user_agent.is_pc # returns False
user_agent.is_bot # returns False
str(user_agent) # returns "iPad / iOS 3.2 / Mobile Safari 4.0.4"

# Kindle Fire's user agent string
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'
user_agent = parse(ua_string)
user_agent.is_mobile # returns False
user_agent.is_tablet # returns True
user_agent.is_touch_capable # returns True
user_agent.is_pc # returns False
user_agent.is_bot # returns False
str(user_agent) # returns "Kindle / Android / Amazon Silk 1.1.0-80"

# Touch capable Windows 8 device
ua_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 False
user_agent.is_tablet # returns False
user_agent.is_touch_capable # returns True
user_agent.is_pc # returns True
user_agent.is_bot # returns False
str(user_agent) # returns "PC / Windows 8 / IE 10"

Running Tests

python -m unittest discover

Changelog

Version 2.2.0 (2020-08-23)

  • ua-parser >= 0.10.0 is required. Thanks @jnozsc!
  • Added get_device(), get_os() and get_browser() instance methods to UserAgent. Thanks @rodrigondec!

Version 2.1 (2020-02-08)

  • python-user-agents now require ua-parser>=0.9.0. Thanks @jnozsc!
  • Properly detect Chrome Mobile browser families. Thanks @jnozsc!

Version 2.0 (2019-04-07)

  • python-user-agents now require ua-parser>=0.8.0. Thanks @IMDagger!

Version 1.1

  • Fixes packaging issue

Version 1.0

  • Adds compatibility with ua-parser 0.4.0
  • Access to more device information in user_agent.device.brand and user_agent.device.model

Version 0.3.2

  • Better mobile detection
  • Better PC detection

Version 0.3.1

  • user_agent.is_mobile returns True when mobile spider is detected

Version 0.3.0

  • Added str/unicode methods for convenience of pretty string

Version 0.2.0

  • Fixed errors when running against newer versions if ua-parser
  • Support for Python 3

Version 0.1.1

  • Added is_bot property
  • Symbian OS devices are now detected as a mobile device

Version 0.1

  • Initial release

Developed by the cool guys at Stamps.

Comments
  • Add Chromebook detection as a

    Add Chromebook detection as a "pc"

    Given Chromebooks are laptops, treat them as is done for MacBooks - "PC" category.

    The new test for this passes but pre-existing test for iPad is failing.

    opened by kulor 9
  • Surface tablets are seen as non-touch-capable pc

    Surface tablets are seen as non-touch-capable pc

    I'm having an issue with a Surface. User agent is Mozilla/5.0 (Windows NT 6.3; ARM; Trident/7.0; Touch; rv:11.0 and it's not detexcted as a touch-capable device:

    {'device': Device(family='Other'), 'browser': Browser(family=u'IE', version=(11,), version_string='11'), 'os': OperatingSystem(family='Windows', version=(), version_string=''), 'ua_string': 'Mozilla/5.0 (Windows NT 6.3; ARM; Trident/7.0; Touch; rv:11.0) like Gecko'}

    opened by qur2 8
  • _is_android_tablet was detecting Opera Mini as Tablet fixed now

    _is_android_tablet was detecting Opera Mini as Tablet fixed now

    Opera Mini mobile browser is wrongly detected as Tablet. is_tablet() internally uses _is_android_tablet() which was detecting "Opera Mini" as Tablet, this is fixed in this pull request.

    opened by shivamsupr 6
  • dont work on ubuntu 14.04

    dont work on ubuntu 14.04

    i install ua-parser and user-agents on ubuntu via pip ...

    but when i run python script always get me error

    ua = parse('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') File "/usr/local/lib/python2.7/dist-packages/user_agents/parsers.py", line 205, in parse return UserAgent(user_agent_string) File "/usr/local/lib/python2.7/dist-packages/user_agents/parsers.py", line 113, in init self.device = parse_device(**ua_dict['device']) TypeError: parse_device() got an unexpected keyword argument 'brand'

    opened by abbas-h 6
  • method __str__() from UserAgent now using methods custom get_device, get_os and get_browser

    method __str__() from UserAgent now using methods custom get_device, get_os and get_browser

    Is better to calculate and provide a method to calculate the string values for device, os and browser instead of doing it directly on __str__ method.

    This way the users may use those methods to get each data directly. Instead of having to make a user_agent.__str__().split('/') to get each value.

    opened by rodrigondec 5
  • Preserve minor and patch version even if they are zeros

    Preserve minor and patch version even if they are zeros

    The current logic drops the minor and patch version if they are zeros.

    E.g. '10.0' becomes '10' and '5.0.0' becomes '5'

    Those info are sometimes useful for doing version comparison

    opened by uaymas 4
  • Fix Firefox Tablet classification.

    Fix Firefox Tablet classification.

    With the latest uap-core changes, Firefox for Android on tablets is identified as Firefox Mobile and therefore we need to update code to check if device is Mobile or Tablet.

    See: https://github.com/ua-parser/uap-core/commit/98d29ff4dbab665e380ef0c7eaeeecb6afce425c

    opened by glogiotatidis 4
  • Android 9 os version empty

    Android 9 os version empty

    Hello,

    When I parse the below user agent, it returns empty. Although it should return 9.

    Dalvik/2.1.0 (Linux; U; Android 9; SM-G965U Build/PPR1.180610.011)

    opened by ardaguclu 3
  • Pin ua-parser to <0.4.0

    Pin ua-parser to <0.4.0

    ua-parser 0.4.0 has just been released, and the lack of pinning here breaks apps that don't explicitly pin their dependencies. Don't know if this is the right place / way to pin this, but it would definitely improve the situation.

    opened by abesto 3
  • mobile spiders should be considered mobile

    mobile spiders should be considered mobile

    Google don't wanna be treated different than a normal user. As google sets its user agent browser to "Mobile Safari" it have to be detected as 'is_mobile'.

    opened by paepke 3
  • Internet Explorer 11

    Internet Explorer 11

    sadly, IE11 is detected as 'other'

    Browser(family='Other', version=(), version_string='')

    here is the link:

    http://msdn.microsoft.com/library/ie/hh869301(v=vs.85).aspx

    opened by abdelouahabb 3
  • PetalBot not identified as bot

    PetalBot not identified as bot

    I have got several accesses from Petal Search bot PetalBot which are not identified as bots. It is identified with following User Agent:

    Mozilla/5.0 (Linux; Android 7.0;) AppleWebKit/537.36 (KHTML, like Gecko) Mobile Safari/537.36 (compatible; PetalBot;+https://webmaster.petalsearch.com/site/petalbot)
    
    opened by PetrDlouhy 0
  • Dalvik browser on Android is identified as a tablet

    Dalvik browser on Android is identified as a tablet

    I just bumped into some user-agents that have an explicit mobile phone name stated but still detected as a tablet. Here are some examples:

    Dalvik/1.6.0 (Linux; U; Android 4.4.4; GT-I9195I Build/KTU84P)
    Dalvik/2.1.0 (Linux; U; Android 10; ASUS_I003DD Build/QKQ1.200419.002)
    Dalvik/2.1.0 (Linux; U; Android 10; BLA-L09 Build/HUAWEIBLA-L09S)
    Dalvik/2.1.0 (Linux; U; Android 10; ELE-L29 Build/HUAWEIELE-L29)
    Dalvik/2.1.0 (Linux; U; Android 10; Redmi Note 8 Pro MIUI/V12.0.8.0.QGGMIXM)
    

    Any idea how to fix this ? I can work on a PR, but I didn't found how to fix this without implementing a list of valid phone names or regexes which seems quite overkill...

    opened by vparpoil 0
  • VIZIO SmartCast User Agents Incorrectly Parsed as PC Devices

    VIZIO SmartCast User Agents Incorrectly Parsed as PC Devices

    VIZIO SmartCast User Agents are being incorrected labeled as with True for the is_pc property in the code below:

    https://github.com/selwin/python-user-agents/blob/862c54baf1e6dd095390a8ebfdd3f5303a5b0a32/user_agents/parsers.py#L264

    Examples of these user agents:

    Mozilla/5.0 (X11; Linux armv7l) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36 CrKey/1.0.999999 VIZIO SmartCast(Conjure/SX7B-2.0.9.0 FW/5.0.5.2 Model/E43-F1)
    Mozilla/5.0 (X11; Linux armv7l) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36 CrKey/1.0.999999 VIZIO SmartCast(Conjure/MTKA-1.4.39.3 FW/4.50.18 Model/D32h-F4),gzip(gfe),gzip(gfe)
    Mozilla/5.0 (X11; Linux armv7l) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 CrKey/1.0.999999 VIZIO SmartCast(Conjure/MTKD-7.520.90.0-qa FW/5.520.23.2-v-2 Model/P65Q9-J01)
    

    It looks like because these user agents satisfies the both the "Linux" and "X11" conditions the logic is incorrectly classifying them as PC.

    opened by jesseginsberg 0
  • Spider/Crawler Google Favicon not detected as  Spider

    Spider/Crawler Google Favicon not detected as Spider

    Google Favicon is the user agent for downloading favicons defined by websites. https://developers.google.com/search/docs/advanced/appearance/favicon-in-search#crawler

    Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)  Chrome/49.0.2623.75 Safari/537.36 Google Favicon
    
    opened by kojibhy 0
Releases(v2.2.0)
  • v2.2.0(Aug 23, 2020)

    • ua-parser >= 0.10.0 is required. Thanks @jnozsc!
    • Added get_device(), get_os() and get_browser() instance methods to UserAgent. Thanks @rodrigondec!
    Source code(tar.gz)
    Source code(zip)
Owner
Selwin Ong
Selwin Ong
基于Pytex的数学建模工具,实现将md文件转换成pdf/tex文档的前后端

Pytex-for-MCM 基于Pytex的数学建模工具,实现将md文件转换成pdf/tex文档的前后端。

3 May 17, 2021
An experimental Fang Song style Chinese font generated with skeleton-tracing and pix2pix

An experimental Fang Song style Chinese font generated with skeleton-tracing and pix2pix, with glyphs based on cwTeXFangSong. The font is optimised fo

Lingdong Huang 98 Jan 07, 2023
You can encode and decode base85, ascii85, base64, base32, and base16 with this tool.

You can encode and decode base85, ascii85, base64, base32, and base16 with this tool.

8 Dec 20, 2022
Simple python program to auto credit your code, text, book, whatever!

Credit Simple python program to auto credit your code, text, book, whatever! Setup First change credit_text to whatever text you would like to credit

Hashm 1 Jan 29, 2022
Wordle strategy: Find frequency of letters appearing in 5-letter words in the English language

Find frequency of letters appearing in 5-letter words in the English language In

Gabriel Apolinário 1 Jan 17, 2022
This repository contains scripts to control a RGB text fan attached to a Raspberry Pi.

RGB Text Fan Controller This repository contains scripts to control a RGB text fan attached to a Raspberry Pi. Setup The Raspberry Pi and RGB text fan

Luke Prior 1 Oct 01, 2021
Answer some questions and get your brawler csvs ready!

BRAWL-STARS-V11-BRAWLER-MAKER-TOOL Answer some questions and get your brawler csvs ready! HOW TO RUN on android: Install pydroid3 from playstore, and

9 Jan 07, 2023
Skype export archive to text converter for python

Skype export archive to text converter This software utility extracts chat logs

Roland Pihlakas open source projects 2 Jun 30, 2022
Extract knowledge from raw text

Extract knowledge from raw text This repository is a nearly copy-paste of "From Text to Knowledge: The Information Extraction Pipeline" with some cosm

Raphael Sourty 10 Dec 03, 2022
Phone Number formatting for PlaySMS Platform - BulkSMS Platform

BulkSMS-Number-Formatting Phone Number formatting for PlaySMS Platform - BulkSMS Platform. Phone Number Formatting for PlaySMS Phonebook Service This

Edwin Senunyeme 1 Nov 08, 2021
Amazing GitHub Template - Sane defaults for your next project!

🚀 Useful README.md, LICENSE, CONTRIBUTING.md, CODE_OF_CONDUCT.md, SECURITY.md, GitHub Issues and Pull Requests and Actions templates to jumpstart your projects.

276 Jan 01, 2023
Widevine KEY Extractor in Python

Widevine Client 3 This was originally written by T3rry7f. This repo is slightly modified version of his repo. This only works on standard Windows! Usa

Vank0n (SJJeon) 68 Dec 29, 2022
A python tool one can extract the "hash" from a WINDOWS HELLO PIN

WINHELLO2hashcat About With this tool one can extract the "hash" from a WINDOWS HELLO PIN. This hash can be cracked with Hashcat, more precisely with

33 Dec 05, 2022
Free & simple way to encipher text

VenSipher VenSipher is a free medium through which text can be enciphered. It can convert any text into an unrecognizable secret text that can only be

3 Jan 28, 2022
Python tool to make adding to your armory spreadsheet armory less of a pain.

Python tool to make adding to your armory spreadsheet armory slightly less of a pain by creating a CSV to simply copy and paste.

1 Oct 20, 2021
Correcting typos in a word based on the frequency dictionary

Auto-correct text Correcting typos in a word based on the frequency dictionary. This algorithm is based on the distance between words according to the

Anton Yakovlev 2 Feb 05, 2022
Athens: a great tool for taking notes and organising knowldge

AthensSyncer Athens is a great tool for taking notes and organising knowldge. But it is a bummer that you cannot use it accross multiple devices. Well

6 Dec 14, 2022
This project is a small tool for processing url-containing texts delivered by HUAWEI Share on Windows.

hwshare_helper This project is a small tool for handling url-containing texts delivered by HUAWEI Share on Windows. config Before use, please install

1 Jan 19, 2022
Export solved codewars kata challenges to a text file.

Codewars Kata Exporter Note:this is not totally my work.i've edited the project to make more easier and faster for me.you can find the original work h

Oussama Ben Sassi 4 Aug 13, 2021
PyNews 📰 Simple newsletter made with python 🐍🗞️

PyNews 📰 Simple newsletter made with python Install dependencies This project has some dependencies (see requirements.txt) that are not included in t

Luciano Felix 4 Aug 21, 2022