Let's take a look at some examples.
1. Open a webpage to retrieve all the content
Copy codeThe Code is as follows:
From urllib import urlopen
Doc = urlopen ("http://www.baidu.com"). read ()
Print doc
2. Get the Http Header
Copy codeThe Code is as follows:
From urllib import urlopen
Doc = urlopen ("http://www.baidu.com ")
Print doc.info ()
Print doc.info (). getheader ('content-type ')
3. Use proxy
1. view environment variables
Copy codeThe Code is as follows:
Print "" n ". join ([" % s = % s "% (k, v) for k, v in OS. environ. items ()])
Print OS. getenv ("http_proxy ")
2. Set Environment Variables
Copy codeThe Code is as follows:
Import OS
OS. putenv ("http_proxy", "http: // proxyaddr: <port> ")
3. Use a proxy
Copy codeThe Code is as follows:
# Use http://www.someproxy.com: 3128 for http proxying
Proxies = {'http': 'http: // www.someproxy.com: 3128 '}
Filehandle = urllib. urlopen (some_url, proxies = proxies)
# Don't use any proxies
Filehandle = urllib. urlopen (some_url, proxies = {})
# Use proxies from environment-both versions are equivalent
Filehandle = urllib. urlopen (some_url, proxies = None)
Filehandle = urllib. urlopen (some_url)