user-agent Python Library
The user-agent library (version 0.1.14) is a Python module designed for generating random, valid User-Agent HTTP headers and `window.navigator` JavaScript objects. It's particularly useful for web scraping or testing scenarios where mimicking various browser and device user agents is required. The library is currently in a beta development status, with its last update on September 10, 2025.
Warnings
- beta The library is currently in 'Development Status :: 4 - Beta', which means its API might not be entirely stable and could undergo changes in future releases. It may not be fully production-ready for critical applications.
- gotcha This library is solely for *generating* user agent strings and navigator objects, not for *parsing* an existing user agent string to extract browser, OS, or device information. Attempting to use its functions for parsing will not work. For parsing, consider libraries like `user-agents` (plural) or `ua-parser`.
- gotcha While the package's metadata indicates compatibility with `Python >=2.7`, Python 2.7 has reached its End-of-Life. Using this library in environments that rely on Python 2.7 might introduce compatibility issues or security vulnerabilities in modern projects. It's recommended to use it with Python 3.x.
- gotcha The `generate_user_agent()` function produces valid, random user agents, but there is no explicit mechanism documented on its PyPI page to ensure these are always the *latest* or most current browser versions. Websites frequently update their anti-bot measures, and using outdated user agents can lead to requests being blocked or flagged.
- gotcha A common mistake in web scraping is to generate a single user-agent string and reuse it for all requests. Even a realistic user-agent will eventually get flagged if it makes too many requests from the same IP address in a short period, signaling automated behavior.
Install
-
pip install user-agent
Imports
- generate_user_agent
from user_agent import generate_user_agent
- generate_navigator
from user_agent import generate_navigator
Quickstart
from user_agent import generate_user_agent, generate_navigator
# Generate a random user agent string
random_ua = generate_user_agent()
print(f"Generated User-Agent: {random_ua}")
# Generate a navigator object (dictionary)
navigator_obj = generate_navigator()
print(f"Generated Navigator Object: {navigator_obj}")
# Example of generating a user agent for a specific OS/browser
mac_firefox_ua = generate_user_agent(os=('mac',), navigator='firefox')
print(f"Mac Firefox User-Agent: {mac_firefox_ua}")