{"name":"python-user-agents: Effortless User Agent String Parsing in Python","description":"python-user-agents is a powerful Python library designed to simplify the identification of devices, such as mobile phones, tablets, and PCs, by parsing browser user agent strings. It allows developers to easily determine device capabilities like touch support and categorize user agents as mobile, tablet, or desktop. This tool is essential for tailoring user experiences based on device characteristics.","github":"https://github.com/selwin/python-user-agents","url":"https://osrepos.com/repo/selwin-python-user-agents","source":"osrepos.com","sourceDescription":"This repository profile is provided by osrepos.com, an open source repository discovery platform.","repositoryProfile":"https://osrepos.com/repo/selwin-python-user-agents","generatedFor":"open source discovery and AI-assisted research","markdown":"https://osrepos.com/repo/selwin-python-user-agents.md","json":"https://osrepos.com/repo/selwin-python-user-agents.json","topics":["python","browser-detection","user-agent-detection","web-development","device-detection","backend","library"],"keywords":["python","browser-detection","user-agent-detection","web-development","device-detection","backend","library"],"stars":null,"summary":"python-user-agents is a powerful Python library designed to simplify the identification of devices, such as mobile phones, tablets, and PCs, by parsing browser user agent strings. It allows developers to easily determine device capabilities like touch support and categorize user agents as mobile, tablet, or desktop. This tool is essential for tailoring user experiences based on device characteristics.","content":"## Introduction\n\n`python-user-agents` is a powerful Python library designed to simplify the identification and detection of devices like mobile phones, tablets, and their capabilities by parsing browser user agent strings. Its primary goal is to reliably determine whether a user agent represents a mobile, tablet, or PC-based device, and if it possesses touch capabilities. The library leverages the robust [ua-parser](https://github.com/ua-parser/uap-python \"GitHub Repository - ua-parser\" target=\"_blank\") for the actual parsing of raw user agent strings.\n\n## Installation\n\nInstalling `python-user-agents` is straightforward. The library is hosted on [PyPI](http://pypi.python.org/pypi/user-agents/ \"PyPI - python-user-agents\" target=\"_blank\") and can be installed using pip:\n\nbash\npip install pyyaml ua-parser user-agents\n\n\nAlternatively, you can obtain the latest source code directly from its [GitHub repository](https://github.com/selwin/python-user-agents \"GitHub Repository - python-user-agents\" target=\"_blank\") and install it manually.\n\n## Examples\n\nThe library provides easy access to various basic information through `browser`, `device`, and `os` attributes. Here's how you can parse a user agent string and access its properties:\n\npython\nfrom user_agents import parse\n\n# iPhone's user agent string\nua_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'\nuser_agent = parse(ua_string)\n\n# Accessing user agent's browser attributes\nprint(user_agent.browser)  # returns Browser(family=u'Mobile Safari', version=(5, 1), version_string='5.1')\nprint(user_agent.browser.family)  # returns 'Mobile Safari'\nprint(user_agent.browser.version)  # returns (5, 1)\nprint(user_agent.browser.version_string)   # returns '5.1'\n\n# Accessing user agent's operating system properties\nprint(user_agent.os)  # returns OperatingSystem(family=u'iOS', version=(5, 1), version_string='5.1')\nprint(user_agent.os.family)  # returns 'iOS'\nprint(user_agent.os.version)  # returns (5, 1)\nprint(user_agent.os.version_string)  # returns '5.1'\n\n# Accessing user agent's device properties\nprint(user_agent.device)  # returns Device(family=u'iPhone', brand=u'Apple', model=u'iPhone')\nprint(user_agent.device.family)  # returns 'iPhone'\nprint(user_agent.device.brand) # returns 'Apple'\nprint(user_agent.device.model) # returns 'iPhone'\n\n# Viewing a pretty string version\nprint(str(user_agent)) # returns \"iPhone / iOS 5.1 / Mobile Safari 5.1\"\n\n\nBeyond basic attributes, `user_agents` also exposes \"sophisticated\" attributes for common device types:\n\n*   `is_mobile`: True if identified as a mobile phone.\n*   `is_tablet`: True if identified as a tablet device.\n*   `is_pc`: True if running a traditional \"desktop\" OS.\n*   `is_touch_capable`: True if the user agent has touch capabilities.\n*   `is_bot`: True if the user agent is a search engine crawler/spider.\n\nHere are more examples demonstrating these advanced attributes:\n\npython\nfrom user_agents import parse\n\n# Let's start from an old, non touch Blackberry device\nua_string = 'BlackBerry9700/5.0.0.862 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/331 UNTRUSTED/1.0 3gpp-gba'\nuser_agent = parse(ua_string)\nprint(user_agent.is_mobile) # returns True\nprint(user_agent.is_tablet) # returns False\nprint(user_agent.is_touch_capable) # returns False\nprint(user_agent.is_pc) # returns False\nprint(user_agent.is_bot) # returns False\nprint(str(user_agent)) # returns \"BlackBerry 9700 / BlackBerry OS 5 / BlackBerry 9700\"\n\n# Now a Samsung Galaxy S3\nua_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'\nuser_agent = parse(ua_string)\nprint(user_agent.is_mobile) # returns True\nprint(user_agent.is_tablet) # returns False\nprint(user_agent.is_touch_capable) # returns True\nprint(user_agent.is_pc) # returns False\nprint(user_agent.is_bot) # returns False\nprint(str(user_agent)) # returns \"Samsung GT-I9300 / Android 4.0.4 / Android 4.0.4\"\n\n# iPad's user agent string\nua_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'\nuser_agent = parse(ua_string)\nprint(user_agent.is_mobile) # returns False\nprint(user_agent.is_tablet) # returns True\nprint(user_agent.is_touch_capable) # returns True\nprint(user_agent.is_pc) # returns False\nprint(user_agent.is_bot) # returns False\nprint(str(user_agent)) # returns \"iPad / iOS 3.2 / Mobile Safari 4.0.4\"\n\n\n## Why Use `python-user-agents`?\n\nThis library is invaluable for developers who need to adapt their web applications or services based on the client's device. By accurately parsing user agent strings, you can:\n\n*   **Enhance User Experience**: Deliver optimized content and layouts for mobile, tablet, or desktop users.\n*   **Improve Analytics**: Gain deeper insights into your audience's device preferences and usage patterns.\n*   **Implement Device-Specific Logic**: Apply different business rules or features depending on the device type or its capabilities, such as touch support.\n*   **Identify Bots**: Easily distinguish between human users and automated crawlers or spiders, which can be crucial for security, analytics, or content delivery.\n\n## Links\n\n*   **GitHub Repository**: [https://github.com/selwin/python-user-agents](https://github.com/selwin/python-user-agents \"GitHub Repository - python-user-agents\" target=\"_blank\")\n*   **PyPI**: [http://pypi.python.org/pypi/user-agents/](http://pypi.python.org/pypi/user-agents/ \"PyPI - python-user-agents\" target=\"_blank\")\n*   **Underlying Parser (ua-parser)**: [https://github.com/ua-parser/uap-python](https://github.com/ua-parser/uap-python \"GitHub Repository - ua-parser\" target=\"_blank\")","metrics":{"detailViews":3,"githubClicks":4},"dates":{"published":null,"modified":"2025-11-19T00:01:40.000Z"}}