What Is a User Agent?#
Browsers identify themselves to websites with a User-Agent
string in the request header. The user agent string contains information about:
- Browser type
- Browser version
- Operating system and platform
Websites use this information to provide the best possible user experience, or sometimes to block certain features or identify automated bots so that they can be excluded from analytics.
User agents come in many forms and can look like this:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0
Learn more about user agents and what they can do.
How to Set User Agent#
Note
Few browsers allow you to set the User-Agent
on the fly, but most allow you to set it at the start of a session. See the table of supported browsers below for details.
For a Session#
How to set the user agent in the beginning of a session:
Python | |
---|---|
1 2 3 4 5 6 7 8 |
|
How it appears in the terminal:
MyUserAgent
On the Fly#
Basic Usage:#
Python | |
---|---|
1 2 3 4 5 6 7 |
|
How it appears in the terminal:
MyUserAgent
Append to Existing User Agent:#
If you want to identify your sessions, for instance to exclude bot traffic from your analytics, you can append the existing user agent with a custom value. Imagine that a browser's default user agent is:
Mozilla/5.0
Let's add a custom value to it:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
How the user agent now appears in the terminal:
Mozilla/5.0 MyUserAgent
Supported Browsers#
Most browsers support setting the User-Agent
at the start of a session, but only a few allow you to set it on the fly:
Case | Chrome | Edge | Firefox | Safari | Internet Explorer |
---|---|---|---|---|---|
For a session | |||||
On the fly |
How to Randomize User Agent#
As example for advanced usage, you can randomize the user agent per session or on the fly.
Per Session#
Example:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
On the Fly#
Example:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|