python requests library user agent

Python Requests Library User Agent

If you are working with the Python Requests library, you can change the user agent header that is sent with your HTTP requests. This can be useful if you want to mimic a different browser or client when making requests to a website.

The user agent header tells the web server what type of browser or client is making the request. By default, the Python Requests library uses a user agent string that identifies itself as "python-requests". However, you may want to change this to something more specific.

Changing the User Agent

To change the user agent header in Python Requests, you can simply set the "User-Agent" header in the headers dictionary of your request:


import requests

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('https://www.example.com', headers=headers)

In this example, we are setting the user agent to be that of Google Chrome on Windows 10.

Multiple Ways to Set User Agent

There are multiple ways to set the user agent in Python Requests:

  • Set the "User-Agent" header directly in the headers dictionary, as shown above.
  • Use the "fake_useragent" library to generate random user agent strings:

from fake_useragent import UserAgent
import requests

ua = UserAgent()
headers = {'User-Agent': ua.random}

response = requests.get('https://www.example.com', headers=headers)
  • Use the "requests_useragents" library to get a list of user agent strings:

from requests_useragents import USER_AGENTS
import random
import requests

user_agent = random.choice(USER_AGENTS)
headers = {'User-Agent': user_agent}

response = requests.get('https://www.example.com', headers=headers)