python 获取cookie

我们用python写网站的自动登录程序的时候需要创建一个cookies,我们可以利用python的cooklib模块。
比如:

#coding:utf-8
import urllib,urllib2,cookielib

cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_path = 'http://xxxxxxxxxx.com/login'

data = {"name":"xxxxxxx","passwd":"xxxxxxxx"}
post_data = urllib.urlencode(data)
request = urllib2.Request(login_path,post_data)
html = opener.open(request).read()
if cj:
print cj
cj.save('cookiefile.txt')

之前用cooklib的方法写了一个人人网的自动登录程序,现在人人网改了登录规则,暂时不能用了,但是可以给你参考下。

最近发现了一个python request的库,还不错

posted @ 2013-04-23 23:31  老王python  阅读(2571)  评论(0编辑  收藏  举报