{"id":6933,"library":"ua-generator","title":"User-Agent Generator","description":"ua-generator is a lightweight Python library designed to generate realistic, random user-agent strings. It supports Python >= 3.9 and operates without external dependencies or the need for external user-agent lists, as its user-agent and platform versions are hardcoded and based on real release data. The library also includes support for Client Hints (Sec-CH-UA fields). It maintains a regular release cadence, primarily for version updates to keep its internal data current, with the latest version being 2.0.25.","status":"active","version":"2.0.25","language":"en","source_language":"en","source_url":"https://github.com/iamdual/ua-generator","tags":["user-agent","web scraping","automation","client hints","http headers"],"install":[{"cmd":"pip install ua-generator","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The primary function `generate` is directly accessible after importing the `ua_generator` module.","symbol":"generate","correct":"import ua_generator\nua = ua_generator.generate()"}],"quickstart":{"code":"import ua_generator\nimport requests\n\n# Basic usage: generate a random user agent\ndef get_random_ua_text():\n    ua = ua_generator.generate()\n    print(f\"Generated User-Agent Text: {ua.text}\")\n    return ua.text\n\n# Advanced usage: generate with specific criteria and get full headers including Client Hints\ndef get_custom_ua_headers(browser_list=None, device_list=None):\n    browser_list = browser_list or ['chrome', 'firefox']\n    device_list = device_list or ['desktop', 'mobile']\n    ua = ua_generator.generate(browser=browser_list, device=device_list)\n    print(f\"Generated User-Agent Text (custom): {ua.text}\")\n    \n    # Use ua.headers.get() for a dictionary of HTTP headers, including Client Hints\n    headers = ua.headers.get()\n    print(f\"Generated HTTP Headers: {headers}\")\n\n    # Example of using with requests (simulating HTTP request)\n    try:\n        response = requests.get('https://httpbin.org/headers', headers=headers)\n        response.raise_for_status() # Raise an exception for HTTP errors\n        print(f\"Requests response (status {response.status_code}): {response.json()['headers']}\")\n    except requests.exceptions.RequestException as e:\n        print(f\"Error making request: {e}\")\n    return headers\n\nif __name__ == '__main__':\n    get_random_ua_text()\n    print(\"\\n---\\n\")\n    get_custom_ua_headers(browser_list=['edge'], device_list=['desktop'])\n    print(\"\\n---\\n\")\n    get_custom_ua_headers(browser_list=['safari', 'firefox'], device_list=['mobile'])","lang":"python","description":"This quickstart demonstrates basic user-agent generation and more advanced customization using browser and device criteria. It also shows how to retrieve a dictionary of HTTP headers, including Client Hints, which can be directly used with HTTP libraries like `requests` for more comprehensive browser emulation. The `ua.text` property provides just the User-Agent string, while `ua.headers.get()` provides a full set of relevant HTTP headers."},"warnings":[{"fix":"Run `pip install -U ua-generator` regularly to update to the latest version, which includes updated user-agent data. Consider automating this check in your development or deployment pipeline.","message":"User-agent strings become outdated over time. To ensure the generated user-agents remain realistic and effective for modern web interactions, it is crucial to periodically upgrade the `ua-generator` library.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Instead of `ua.text`, use `ua.headers.get()` to retrieve a dictionary of all relevant HTTP headers, including Client Hints. This dictionary can be passed directly to HTTP client libraries (e.g., `requests`, `httpx`) or used to configure browser automation tools (e.g., `selenium` with CDP commands) for a more complete and accurate user-agent profile. Version 2.0.17 introduced `ua.navigator.get()` specifically for Selenium's `NavigatorUAData` fields.","message":"For comprehensive browser emulation, especially in environments like Selenium or when interacting with services that utilize User-Agent Client Hints, relying solely on the `ua.text` string may be insufficient. Modern browsers transmit additional `Sec-CH-UA` headers.","severity":"gotcha","affected_versions":"All versions prior to 2.0.17, and users of 2.0.17+ who only use `ua.text`."}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}