Latest User Agents

0.0.5 · active · verified Fri Apr 17

`latest-user-agents` is a Python library (version 0.0.5) that provides a simple function to retrieve a list of up-to-date user agent strings for major browsers and operating systems. It fetches these lists dynamically from an external website, caches them, and makes them available for use in web scraping or testing. The project is in maintenance, with no recent updates, but its core functionality remains stable.

Common errors

Warnings

Install

Imports

Quickstart

Fetches a list of current user agent strings and demonstrates selecting a random one. It includes a check for an empty list, which can occur if the external source is unavailable. Note that `requests` is implicitly required for internal fetching and explicitly for the example.

import random
from latest_user_agents import get_latest_user_agents

# Get a list of the latest user agents
user_agents = get_latest_user_agents()

if user_agents:
    # Select a random user agent from the list
    random_ua = random.choice(user_agents)

    print(f"Total user agents fetched: {len(user_agents)}")
    print(f"Random user agent: {random_ua}")

    # Example of using it with requests (requires 'requests' to be installed)
    # import requests
    # try:
    #     response = requests.get('http://httpbin.org/user-agent', headers={'User-Agent': random_ua})
    #     response.raise_for_status()
    #     print(f"Response from httpbin: {response.json()}")
    # except Exception as e:
    #     print(f"Error fetching from httpbin: {e}")
else:
    print("Could not fetch user agents. The list is empty.")

view raw JSON →