Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 - Add custom headers to urllib.request Request

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)' }
like image 488
rovyko Avatar asked Oct 31 '17 06:10

rovyko


1 Answers

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

like image 193
rovyko Avatar answered Nov 11 '22 10:11

rovyko