Python 访问http, https请求
import cookielibimport urllib2
from urllib2 import *
from cStringIO import StringIO
from gzip import GzipFile
cj = cookielib.CookieJar()
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password('PMI',
uri='host-url ', #like: 'www.baidu.com'
user='user-name',
passwd='password')
opener = urllib2.build_opener(urllib2.HTTPSHandler(), auth_handler, urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
req = Request('*******')
req.add_header('Accept-Encoding', 'gzip, deflate') # 用于设置返回的数据是gzip文件,如果是普通的html不需要设置这个内容。
handle = urllib2.urlopen(req)
data = handle.read()
io = StringIO(data)
data = GzipFile('', 'r', 0, io).read()
print data
页:
[1]