GetUserAgent - Random/Fake/Spoof Common User Agents
Table of Contents
Intro
This is just a simple python module to produce a random, commonly used user agents each time. It contains 11 lists for a total of 1,100 user agents. You can choose and combine lists depending on your needs.
Installation
Assumes you have python3 installed.
pip install getuseragent
Features
- Get a random user agent of Chrome, Firefox, Safari, Edge, IE, Android, iOS, Windows, Mac, Linux, Desktop, Mobile, Bots, or any combination
- User-Agents from 100 of the most commonly used
- Add requests handler prefix
- Spoofed or fake user agents
- Combine multiple user agent lists
Use Cases
- Check that your website statistics are identifying browsers correctly
- Handle bots visiting your website
- Making a security system to detect fake user agents?
Code Examples
Initialisation
from getuseragent import UserAgent
useragent = UserAgent()
theuseragent = useragent.Random()
print(theuseragent)
Example output:
Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148
User-Agents Available
Each of the following lists contains 100 commonly used user agents for that software/device/system.
useragent = UserAgent("firefox")
useragent = UserAgent("chrome")
useragent = UserAgent("safari")
useragent = UserAgent("ie")
useragent = UserAgent("edge")
useragent = UserAgent("ios")
useragent = UserAgent("android")
useragent = UserAgent("windows")
useragent = UserAgent("mac")
useragent = UserAgent("linux")
Sets including multiple lists:
useragent = UserAgent("desktop")
useragent = UserAgent("mobile")
useragent = UserAgent()
useragent = UserAgent("all")
Bots like web crawlers:
useragent = UserAgent("bots")
Combination
You can combine lists and sets together. Use the + sign between list names. Duplicate lists will automatically be removed so there's only 1 of each list at most.
Examples:
useragent = UserAgent("chrome+edge+ios")
useragent = UserAgent("mobile+firefox")
useragent = UserAgent("all+bots")
print(useragent.Random())
Print Random User Agent From List
You can print a single random user agent like so:
useragent = UserAgent()
ua = useragent.Random()
print(ua)
Performance / Limiting
You can limit the total amount of user agents in the list to save memory. The default is no limit, and each list has 100 commonly used user agents.
useragent = UserAgent("chrome+firefox+ios")
useragent = UserAgent("chrome+firefox+ios", total=50)
useragent = UserAgent("firefox+safari", limit=10)
useragent = UserAgent("android+ie", limit=10, total=15)
print(useragent.Random())
Requests Handler
You can enable the option to return your user agents ready to be used with the requests module.
import requests
from getuseragent import UserAgent
myuseragent = UserAgent("all", requestsPrefix=True).Random()
page = requests.get("https://google.com", headers=myuseragent)
To Do
- Error handling
- Retrieve updated, most popular user-agents from the internet
- Better performance
- Hot-swappable lists
- Custom list(s)
- Command line arguments
- Improve Readme
Changes
1. Added individual list limits, which can be used when using one or multiple lists.
For example, if you used:
UserAgent("firefox+ios", limit=5)
It would limit each list to 5 user agents, and two lists, bringing the total user agents 10.
2. Checks if user agent files exist.
3. Checks if user list empty in Random()
Fixed package errors, spelling mistakes
Initial release