The Python Oracle

Sending "User-agent" using Requests library in Python

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping

--

Chapters
00:00 Question
00:43 Accepted answer (Score 456)
01:48 Answer 2 (Score 93)
02:13 Answer 3 (Score 7)
02:31 Thank you

--

Full question
https://stackoverflow.com/questions/1060...

Accepted answer links:
[list of HTTP header fields]: https://en.wikipedia.org/wiki/List_of_HT...
[request-specific fields]: https://en.wikipedia.org/wiki/List_of_HT...

Answer 2 links:
[session]: https://2.python-requests.org/en/master/...
[this question]: https://stackoverflow.com/questions/1703...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #webcrawler #pythonrequests

#avk47



ACCEPTED ANSWER

Score 496


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 requests

url = 'SOME URL'

headers = {
    'User-Agent': 'My User Agent 1.0',
    'From': 'youremail@domain.example'  # 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 requests

url = 'SOME URL'

# Get a copy of the default headers that requests would use
headers = 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)



ANSWER 2

Score 104


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('https://httpbin.org/headers')

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




ANSWER 3

Score 17


It will send the request like browser

import requests

url = 'https://Your-url'
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'}

response= requests.get(url.strip(), headers=headers, timeout=10)



ANSWER 4

Score 1


alternative for session.headers.update()

session = requests.Session()
session.headers['User-Agent'] = 'Custom user agent'

session.get('https://httpbin.org/headers')