1. 参考
2. 代码
(1) python2
1 # python22 import httplib3 import urllib4 httplib.HTTPConnection.debuglevel = 15 response = urllib.urlopen('http://www.baidu.com').read()
(2) 兼容python2和python3
1 import sys 2 try: 3 import urllib.request as urllib_request #python3 4 except: 5 import urllib2 as urllib_request #python2 6 print(sys.version) 7 httpHandler = urllib_request.HTTPHandler(debuglevel=1) 8 httpsHandler = urllib_request.HTTPSHandler(debuglevel=1) 9 opener = urllib_request.build_opener(httpHandler, httpsHandler)10 print(opener.open('https://httpbin.org/user-agent').read().decode('utf-8'))11 #install_opener之后才能全局12 urllib_request.install_opener(opener)13 response = urllib_request.urlopen('http://www.baidu.com')