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.
pip install fake-useragentVerified import paths — ran on the pinned version, not inferred.
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.
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').
Replace `platform='pc'` with `platform='desktop'` when calling methods like `ua.get_ua()` to specify desktop user agents.
Remove `min_percentage` from your calls, as it will not filter user agents based on usage. Rely on other parameters for selection.
Upgrade your Python environment to version 3.9 or newer if you are using fake-useragent 2.0.1 or later.
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`.
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.pip install fake-useragent
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')`
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.
Explicitly include the fake-useragent data files when building with PyInstaller: `pyinstaller --onefile --collect-datas=fake_useragent your_script.py`
No dependency data recorded yet.