4 >>> full_url =url +'? '+url_values >>> data =urllib.request.urlopen(full_url) The response also has two useful methods. See the section on info and geturl which comes after we have a look at what happens when things go wrong. urlopen raises$_{URLError}$ when it cannot handle a response (though as usual with Python APIs, built-in exceptions such as$_{ValueError}$,$_{TypeError}$ etc. may also be raised). HTTPError is the subclass of$_{URLError}$ raised in the specific case of HTTP URLs. The exception classes are exported from the$_{urllib.error}$ module. Some websites 1 dislike being browsed by programs, or send different versions to different browsers$^{2}$. By default urllib identifies itself as$_{Python-urllib/x.y}$ (where$_{x}$and$_{y}$ are the major and minor version numbers of the Python release, e.g. Python-urllib/2.5 ), which may confuse the site, or just plain not work. The way a browser identifies itself is through the$_{User-Agent}$header$^{3}$. When you create a Request object you can pass a dictionary of headers in. The following example makes the same request as above, but identifies itself as a version of Internet Explorer$^{4}$. We’ll discuss here one particular HTTP header, to illustrate how to add headers to your HTTP request. Notice that the full URL is created by adding a ? to the URL, followed by the encoded values. 2.2 Headers 3 Handling Exceptions 1 Google for example. 2 Browser sniffing is a very bad practice for website design - building sites using web standards is much more sensible. Unfortunately a lot of sites still send different versions to different browsers. 3 The user agent for MSIE 6 is ‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)’ 4 For details of more HTTP request headers, see Quick Reference to HTTP Headers. (continued from previous page) import urllib.parse import urllib.request url ='http://www.someserver.com/cgi-bin/register.cgi ' user_agent='Mozilla/5.0 (Windows NT 6.1; Win64; x64) ' values={ 'name':'Michael Foord ', 'location':'Northampton', 'language ':'Python'} headers ={ 'User-Agent': user_agent} data =urllib.parse.urlencode(values) data =data.encode('ascii ') req =urllib.request.Request(url, data, headers) with urllib.request.urlopen(req) as response: the_page=response.read()