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)
1>>> import urllib.request
2>>> #urllib.request.urlretrieve(url, filename=None, reporthook=None, data=None)
3#Copy a network object denoted by a URL to a local file
4>>> local_filename, headers = urllib.request.urlretrieve('http://python.org/')
5>>> html = open(local_filename)
6>>> html.close()
7
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