What is user-agents?
The 'user-agents' npm package is designed to generate random user-agent strings that can be used for web scraping, testing, or any other scenario where you need to simulate different browsers and devices.
What are user-agents's main functionalities?
Generate a random user-agent
This feature allows you to generate a random user-agent string. The generated user-agent string can be used to simulate different browsers and devices.
const UserAgent = require('user-agents');
const userAgent = new UserAgent();
console.log(userAgent.toString());
Generate a user-agent with specific options
This feature allows you to generate a user-agent string with specific options, such as targeting mobile devices. This can be useful for testing how your application behaves on different types of devices.
const UserAgent = require('user-agents');
const userAgent = new UserAgent({ deviceCategory: 'mobile' });
console.log(userAgent.toString());
Access user-agent properties
This feature allows you to access detailed properties of the generated user-agent, such as browser name, version, platform, and more. This can be useful for logging or debugging purposes.
const UserAgent = require('user-agents');
const userAgent = new UserAgent();
console.log(userAgent.data);
Other packages similar to user-agents
random-useragent
The 'random-useragent' package also generates random user-agent strings. It provides a simpler API compared to 'user-agents' and is easier to use for basic use cases. However, it lacks the advanced customization options available in 'user-agents'.
useragent
The 'useragent' package is primarily focused on parsing and identifying user-agent strings rather than generating them. It can be used to analyze user-agent strings and extract information about the browser, version, and platform. It complements 'user-agents' by providing analysis capabilities.
ua-parser-js
The 'ua-parser-js' package is a JavaScript library for parsing user-agent strings and extracting detailed information about the browser, engine, OS, CPU, and device. While it does not generate user-agent strings, it is useful for analyzing and understanding user-agent data.
User Agents
User Agents is a JavaScript module for generating random user agents according to how frequently they're used in the wild.
This is useful for web scraping because it's often desired to create realistic traffic patterns.
There are several other similar projects for generating random user agents, but User Agents is the only one that automatically publishes new versions with updated data on a regular basis.
This ensures that your user agents will always be realistic and up to date.
Installation
The User Agents package is available on npm with the package name user-agents.
You can install it using your favorite JavaScript package manager in the usual way.
yarn add user-agents
Usage
The userAgents.random()
method can be used to generate a random user agent.
The most common user agents are weighted with higher probabilities so that the randomly generated user agents will follow realistic probability distributions.
For example, the following code snippet will generate five random user agents based on these probabilities.
const userAgents = require('user-agents');
for (let i = 0; i < 5; i++) {
const userAgent = userAgents.random();
console.log(userAgents);
};
The output will be random, and will also depend on which package version you're using, but a typical output would look something like this.
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/61.0
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
Acknowledgements
The user agent frequency data used in this library is currently provided by the Will's House Tech Blog.
Contributing
Contributions are welcome, but please follow these contributor guidelines outlined in CONTRIBUTING.md.
License
Slice is licensed under a BSD 2-Clause License and is copyright Intoli, LLC.