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:
Learn more here.
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 below for details.
For a Session#
How to set the user agent in the beginning of a session:
Python | |
---|---|
How it appears in the terminal:
On the Fly#
Basic Usage:#
Python | |
---|---|
How it appears in the terminal:
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:
Let's add a custom value to it:
Python | |
---|---|
How the user agent now appears in the terminal:
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 | |
---|---|
On the Fly#
Example: