urllib
urllib.request.urlopen
from urllib import request

with request.urlopen('http://python.org/') as response:
   html = response.read()
   print(html)
		
urllib.request.Request
import urllib.request

req = urllib.request.Request('http://www.voidspace.org.uk') # Request represents the HTTP request
with urllib.request.urlopen(req) as response: # response is a file-like object
   the_page = response.read()
		
Data
import urllib.parse
import urllib.request

url = 'https://www.w3schools.com/action_page.php'
headers = {
        "User-Agent" : "Mozilla/5.0 (X11; U; Linux i686; " + \
                "en-US; rv:1.9.2.24) Gecko/20111107 " + \
                "Linux Mint/9 (Isadora) Firefox/3.6.24",
                }

data = {'firstname':'Mickey', 'lastname':'Mouse'}
data = urllib.parse.urlencode(data)
data = data.encode('ascii')

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

html = response.read()
print(html)
		
Headers
import urllib.request

url = 'https://www.redfin.com/city/30818'
headers = {
        "User-Agent" : "Mozilla/5.0 (X11; U; Linux i686; " + \
                "en-US; rv:1.9.2.24) Gecko/20111107 " + \
                "Linux Mint/9 (Isadora) Firefox/3.6.24",
                }

data = {}
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req)

html = response.read()
print(html)
		
URLError
import urllib.request

req = urllib.request.Request('http://www.pretend_server.org')
try: 
    response = urllib.request.urlopen(req)
except urllib.error.URLError as e:
    print(e.reason)
		
Reference
  • Python3 Howto