One, open a Web page to get all the content
From urllib import Urlopen
Doc = urlopen("http://www.baidu.com"). Read()
Print Doc
Second, get HTTP headers
From urllib import Urlopen
Doc = urlopen("http://www.baidu.com")
Print doc. Info()
Print doc. Info(). GetHeader(' Content-type ')
Third, the use of agents
1. View Environment variables
Print "" n". Join(["%s=%s" % (k, v) for K, v in os. Environ. Items()])
Print os. getenv("Http_proxy")
2. Setting Environment variables
Import OS
Os. Putenv("Http_proxy", "http://proxyaddr:<port>")
3. Use of Proxy
# 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 )