Python 3 - Add custom headers to urllib.request Request
Asked Answered
S

3

22

In Python 3, the following code obtains the HTML source for a webpage.

import urllib.request
url = "https://docs.python.org/3.4/howto/urllib2.html"
response = urllib.request.urlopen(url)

response.read()

How can I add the following custom header to the request when using urllib.request?

headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }
Stalemate answered 31/10, 2017 at 6:40 Comment(0)
S
42

The request headers can be customized by first creating a request object then supplying it to urlopen.

import urllib.request
url = "https://docs.python.org/3.4/howto/urllib2.html"
hdr = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }

req = urllib.request.Request(url, headers=hdr)
response = urllib.request.urlopen(req)
response.read()

Source: Python 3.4 Documentation

Stalemate answered 31/10, 2017 at 6:40 Comment(0)
C
6
import urllib.request

opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
response = urllib.request.urlopen("url")
response.read()

Should you wish to learn about the details you can refer to the python documentation: https://docs.python.org/3/library/urllib.request.html

Cod answered 25/4, 2018 at 11:39 Comment(0)
C
1
#Using urllib.request, with urlopen, allows to open the specified URL.
#Headers can be included inside the urlopen along with the url.

from urllib.request import urlopen
url = "https://docs.python.org/3.4/howto/urllib2.html"
header = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }
response = urlopen(url, headers=header)
response.read()
Clower answered 6/2, 2021 at 18:38 Comment(2)
Please don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.Y
Downvoting, as headers parameter is not supported in python 3: docs.python.org/3/library/…Hardej

© 2022 - 2024 — McMap. All rights reserved.