python requests set user agent

Python Requests Set User Agent

Setting user agents in Python Requests is a way to identify the client making the request. A user agent string is a piece of information that is sent along with every HTTP request. It identifies the client application or software that is making the request. Setting a user agent allows websites to know what kind of device or browser you are using and can adapt their responses accordingly.

Using Headers

You can set a custom user agent using the headers parameter in the requests.get() method. Here's an example:


import requests

url = 'https://www.example.com'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)

print(response.content)
    

The headers parameter expects a dictionary of strings as input, where the key is the header name and the value is the header value. In this case, we are setting the User-Agent header to a string that identifies our application as Google Chrome running on Windows 10.

Using a Session

If you want to use the same user agent for all requests, you can create a session object and set the user agent for it. Here's an example:


import requests

s = requests.Session()
s.headers.update({'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'})

response1 = s.get('https://www.example.com/page1')
response2 = s.get('https://www.example.com/page2')
response3 = s.get('https://www.example.com/page3')

print(response3.content)
    

In this example, we are creating a session object and setting the User-Agent header for it. We can then make multiple requests using the same session object, and the user agent will be sent with each request.