1try:
2 from urllib.request import Request, urlopen # Python 3
3except ImportError:
4 from urllib2 import Request, urlopen # Python 2
5
6req = Request('http://api.company.com/items/details?country=US&language=en')
7req.add_header('apikey', 'xxx')
8content = urlopen(req).read()
9
10print(content)
1import urllib.request
2
3req = urllib.request.Request('http://www.voidspace.org.uk')
4with urllib.request.urlopen(req) as response:
5 the_page = response.read()
6
1#In Python 3.x, the urlretrieve function is located in the urllib.request module:
2from urllib.request import urlretrieve