`
zl198751
  • 浏览: 272070 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Python 访问http, https请求

阅读更多

import cookielib
import 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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics