Categories
Python Answers

How to send User-agent using Requests library in Python?

Spread the love

Sometimes, we want to send User-agent using Requests library in Python.

In this article, we’ll look at how to send User-agent using Requests library in Python.

How to send User-agent using Requests library in Python?

To send User-agent using Requests library in Python, we can add the User-Agent request header is the headers.

For instance, we write

import requests

url = "https://example.com"

headers = {
    "User-Agent": "My User Agent 1.0",
}

response = requests.get(url, headers=headers)

to create the headers dict that has the User-Agent request header key and value.

Then we call requests.get with the url and the headers to make a GET request to the url with the user agent header.

Conclusion

To send User-agent using Requests library in Python, we can add the User-Agent request header is the headers.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *