python cookielib

# HttpClient.py is written by [xqin]: https://github.com/xqin/SmartQQ-for-Raspberry-Pi
import cookielib, urllib, urllib2


import urllib2,cookielib

cookie=cookielib.CookieJar() #创建cooke实例
cookiehand=urllib2.HTTPCookieProcessor(cookie) #生成cooke处理手柄
opener=urllib2.build_opener(cookiehand) ##创建自定义的opener,引入cooke处理手柄
opener.addheaders = [  ##openerp默认头文件
('Accept', 'application/javascript, */*;q=0.8'),
('User-Agent', 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)')
]

urllib2.install_opener(opener) ##urlli2 替换urllib2默认的 urlopen 方法


req = urllib2.Request('http://www.baidu.com')
res= urllib2.urlopen(req)
for i in cookie:
print 'name: %s, value:%s ' % (i.name, i.value) #显示cook的内容

posted on 2016-06-25 11:37  Odoo在中国  阅读(236)  评论(0编辑  收藏  举报

导航