开发者

cookielib: How to save Python cookies and load it?

开发者 https://www.devze.com 2023-01-30 02:17 出处:网络
I am using CentOS 5.5 and Python. The following code works perfectly, cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.开发者_运维百科HTTPCookieProcessor(cj))

I am using CentOS 5.5 and Python.

The following code works perfectly,

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.开发者_运维百科HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : 'myname', 'password' : '123456'})
resp = opener.open('http://www.example.com/signin.html', login_data)

However, I want to save the cookies to /var/www/html/cookies and load it when I need, so I don't have to sign in every time, just like the browsers do.

How can I save and load the cookies? Thanks a lot!


cookielib defines a separate FileCookieJar class that has the functionality you're looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消