Official doc: howto, library

0. Import

Usually we should import urllib and urllib2:

import urllib

import urllib2

1. Most Simple Way

The most simple way to get HTML is:

import urllib2
response = urllib2.urlopen(‘ ‘)
html =

2. Sending Request/Data 

Open a request instead of a URL string. You can also add request etc. in the request. Below is the most simple way of sending a request:

req = urllib2.Request(‘ ‘)
response = urllib2.urlopen(req)
the_page =

Add data:

url = ‘ ‘
values = {‘name’ : ‘Michael Foord’,
‘location’ : ‘Northampton’,
‘language’ : ‘Python’ }

data = urllib.urlencode(values)      # data (which appears as a dictionary) must be encoded to serve as a parameter for urllib2.Request
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page =

In general, the parameters of urllib2.Request include url, data, header, etc. More usages here.