{"id":1479,"library":"fake-useragent","title":"fake-useragent","description":"fake-useragent is a Python library that generates up-to-date and realistic browser user-agent strings for web scraping and testing purposes. It dynamically fetches a database of real-world user agents to provide diverse and current options. The current version is 2.2.0, with minor releases occurring every few months to update the user-agent database or fix small issues.","status":"active","version":"2.2.0","language":"python","source_language":"en","source_url":"https://github.com/fake-useragent/fake-useragent","tags":["user-agent","web scraping","faker","http","browser"],"install":[{"cmd":"pip install fake-useragent","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"UserAgent","correct":"from fake_useragent import UserAgent"},{"note":"Exception for handling errors like network issues during data fetching.","symbol":"FakeUserAgentError","correct":"from fake_useragent import FakeUserAgentError"}],"quickstart":{"code":"from fake_useragent import UserAgent, FakeUserAgentError\n\ntry:\n    ua = UserAgent()\n    print(f\"Random User-Agent: {ua.random}\")\n    print(f\"Chrome User-Agent: {ua.chrome}\")\n    print(f\"Firefox User-Agent: {ua.firefox}\")\n    print(f\"User-Agent for macOS: {ua.get_ua('chrome', os='macos')}\")\n    print(f\"User-Agent for desktop: {ua.get_ua(platform='desktop')}\")\nexcept FakeUserAgentError as e:\n    print(f\"Error fetching user agents: {e}\")\n    print(\"This might be due to network issues or the first-time data download failing.\")","lang":"python","description":"This quickstart demonstrates how to initialize `UserAgent` and retrieve various types of user-agent strings. It includes basic error handling for potential network issues during the initial data download or cache refresh."},"warnings":[{"fix":"Ensure all OS and browser option parameters (e.g., in `ua.get_ua(browser='Chrome', os='Windows')`) are passed with the correct casing as specified in the documentation (e.g., 'chrome', 'firefox', 'windows', 'macos').","message":"Since version 2.0.0, OS and browser options are case-sensitive. Previously, they might have been implicitly converted or handled with more flexibility.","severity":"breaking","affected_versions":"2.0.0+"},{"fix":"Replace `platform='pc'` with `platform='desktop'` when calling methods like `ua.get_ua()` to specify desktop user agents.","message":"In version 2.0.0, the `platform` type value 'pc' was changed to 'desktop'. If you were specifically requesting desktop user agents using `platform='pc'`, your code will no longer work as expected.","severity":"breaking","affected_versions":"2.0.0+"},{"fix":"Remove `min_percentage` from your calls, as it will not filter user agents based on usage. Rely on other parameters for selection.","message":"Since version 1.5.0, the `min_percentage` parameter for filtering user agents is effectively useless. The new data sources introduced in this version and later do not provide usage statistics, so this filter is ignored.","severity":"gotcha","affected_versions":"1.5.0+"},{"fix":"Upgrade your Python environment to version 3.9 or newer if you are using fake-useragent 2.0.1 or later.","message":"fake-useragent requires Python 3.9 or newer since version 2.0.1. Attempting to use it with older Python versions will result in compatibility errors.","severity":"gotcha","affected_versions":"2.0.1+"},{"fix":"Ensure reliable network access for initialization. Handle `FakeUserAgentError` for network-related failures. For restricted environments, consider pre-caching the data or using options like `cache=False` (though this fetches data on every call) or `use_cache_server=False`.","message":"On its first run (or when the cache expires), `fake-useragent` attempts to download a JSON file (~500KB) from an external source (currently intoli.com). This can introduce network delays, fail due to connectivity issues, or be blocked by firewalls/proxies.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Replace calls to `ua.get_ua()` with direct attribute access like `ua.random`, `ua.chrome`, `ua.firefox`, or use `ua.get('browser_name')` for dynamic selection.","message":"The `get_ua()` method was removed. Attempting to call `ua.get_ua()` will result in a `TypeError: 'str' object is not callable` as it is no longer a callable method.","severity":"breaking","affected_versions":"2.0.0+"}],"env_vars":null,"search_vec":"'2.2.0':51 'agent':22,40,64,72 'browser':19,77 'current':45,48 'databas':34,65 'date':16 'divers':43 'dynam':31 'everi':56 'fake':2,5 'fake-userag':1,4 'faker':75 'fetch':32 'fix':67 'generat':12 'http':76 'issu':69 'librari':10 'minor':53 'month':58 'occur':55 'option':46 'provid':42 'purpos':29 'python':9 'real':37 'real-world':36 'realist':18 'releas':54 'scrape':26,74 'small':68 'string':23 'test':28 'up-to-d':13 'updat':60 'user':21,39,63,71 'user-ag':20,62,70 'userag':3,6 'version':49 'web':25,73 'world':38","created_at":"2026-04-09T03:49:42.579038+00:00","updated_at":"2026-04-16T14:55:13.217432+00:00","problems":[{"fix":"pip install fake-useragent","cause":"The 'fake-useragent' library is not installed in the Python environment, or the Python interpreter being used cannot find it.","error":"ModuleNotFoundError: No module named 'fake_useragent'"},{"fix":"Initialize the UserAgent with a fallback string: `from fake_useragent import UserAgent; ua = UserAgent(fallback='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3')`","cause":"The library failed to download user-agent data from its remote sources after several attempts, typically due to network issues, temporary downtime of the data source, or being blocked.","error":"fake_useragent.errors.FakeUserAgentError: Maximum amount of retries reached"},{"fix":"Update the fake-useragent library to the latest version (`pip install --upgrade fake-useragent`), which often includes fixes for changes in data sources. Alternatively, use a `fallback` user agent if updates do not resolve the issue.","cause":"This error often occurs when the HTML structure of the external data source from which fake-useragent scrapes its data has changed, causing the library's parsing logic to fail.","error":"IndexError: list index out of range"},{"fix":"Explicitly include the fake-useragent data files when building with PyInstaller: `pyinstaller --onefile --collect-datas=fake_useragent your_script.py`","cause":"When packaging a Python application with PyInstaller, the internal data files (e.g., 'browsers.json') required by fake-useragent might not be correctly bundled.","error":"ModuleNotFoundError: No module named 'fake_useragent.data'"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"2.2.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/fake-useragent/fake-useragent","docs":null,"changelog":null,"pypi":"https://pypi.org/project/fake-useragent/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing","http-networking"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":"verified"}}