获取访问网页返回的cookie,打印每个cookie的域值

#模拟登陆百度空间,获得最开始登陆百度空间网页返回的cookie
import cookielib,urllib,urllib2

loginUrl='http://hi.baidu.com/motionhouse'
cj=cookielib.CookieJar()  #新建CookieJar实例,用于保存cookie
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))  #处理cookie并生成opener
urllib2.install_opener(opener)  #安装使用opener
resp=urllib2.urlopen(loginUrl)  #打开地址
for index,cookie in enumerate(cj):
    print '[',index,']',cookie
    print cookie.name
    print cookie.value
    print cookie.expires
    print cookie.path
    print cookie.comment
    print cookie.domain
    #print cookie.max-age
    print cookie.secure
    print cookie.version
    #print cookie.httponly

posted @ 2014-03-23 15:13  junezhang  阅读(1382)  评论(0编辑  收藏  举报