This Python library provides lists of user agents based on filters such as operating system, software name, and popularity. It currently holds a collection of over 326,000 user agents. The library's last release was version 1.0.1 in December 2018, making its user agent data significantly outdated for modern web scraping and automation tasks.
pip install random_user_agentVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to import the UserAgent class and parameter enums, initialize a user agent rotator with specific browser and operating system filters, retrieve a list of user agents, and get a single random user agent.
Consider using actively maintained libraries like 'fake-useragent' or 'ua-generator' that provide up-to-date user agent lists or dynamically generate them. Always verify the recency of user agent data if using any random user agent generator.
Assess the critical need for up-to-date user agents. If your application relies on current browser and OS identities, this library is unsuitable. Migrate to a well-maintained alternative.
Install the library using pip: `pip install random-user-agent`
Import the `UserAgent` class from its correct submodule path: `from random_user_agent.user_agent import UserAgent`
First, import and instantiate the `UserAgent` class, then call the method on the instance: ```python from random_user_agent.user_agent import UserAgent ua_rotator = UserAgent() user_agent = ua_rotator.get_random_user_agent() ```
Ensure filter values are correct (e.g., use `SoftwareName.CHROME.value`), broaden the filters to ensure a non-empty list, or add a check before calling `get_random_user_agent()`:
```python
from random_user_agent.user_agent import UserAgent
from random_user_agent.params import SoftwareName, OperatingSystem
user_agent_rotator = UserAgent(
software_names=[SoftwareName.CHROME.value], # Use .value for enum values
operating_systems=[OperatingSystem.WINDOWS.value]
)
# Add a check to prevent IndexError
if user_agent_rotator.user_agents_list:
user_agent = user_agent_rotator.get_random_user_agent()
else:
print("No user agents found with the specified filters.")
```No dependency data recorded yet.