fake-useragent

2.2.0 · active · verified Thu Apr 09

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.

Warnings

Install

Imports

Quickstart

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.

from fake_useragent import UserAgent, FakeUserAgentError

try:
    ua = UserAgent()
    print(f"Random User-Agent: {ua.random}")
    print(f"Chrome User-Agent: {ua.chrome}")
    print(f"Firefox User-Agent: {ua.firefox}")
    print(f"User-Agent for macOS: {ua.get_ua('chrome', os='macos')}")
    print(f"User-Agent for desktop: {ua.get_ua(platform='desktop')}")
except FakeUserAgentError as e:
    print(f"Error fetching user agents: {e}")
    print("This might be due to network issues or the first-time data download failing.")

view raw JSON →