Sending "User-agent" using Requests library in Python Sending "User-agent" using Requests library in Python python python

Sending "User-agent" using Requests library in Python

The user-agent should be specified as a field in the header.

Here is a list of HTTP header fields, and you'd probably be interested in request-specific fields, which includes User-Agent.

If you're using requests v2.13 and newer

The simplest way to do what you want is to create a dictionary and specify your headers directly, like so:

import requestsurl = 'SOME URL'headers = {    'User-Agent': 'My User Agent 1.0',    'From': ''  # This is another valid field}response = requests.get(url, headers=headers)

If you're using requests v2.12.x and older

Older versions of requests clobbered default headers, so you'd want to do the following to preserve default headers and then add your own to them.

import requestsurl = 'SOME URL'# Get a copy of the default headers that requests would useheaders = requests.utils.default_headers()# Update the headers with your custom ones# You don't have to worry about case-sensitivity with# the dictionary keys, because default_headers uses a custom# CaseInsensitiveDict implementation within requests' source code.headers.update(    {        'User-Agent': 'My User Agent 1.0',    })response = requests.get(url, headers=headers)

It's more convenient to use a session, this way you don't have to remember to set headers each time:

session = requests.Session()session.headers.update({'User-Agent': 'Custom user agent'})session.get('')

By default, session also manages cookies for you. In case you want to disable that, see this question.

simply you can do it like below:

import requestsurl ="URL", headers={"FUser":"your username","FPass":"your password","user-agent": "your custom text for the user agent "})