如何将cookie插入urllib2 openerdirector对象?

时间:2010-12-01 00:57:53

标签: python cookies urllib2

有没有办法将cookie附加到已经制作和使用过的openerdirector对象中?

1 个答案:

答案 0 :(得分:0)

试试这个:

import urllib2
import cookielib

# load cookies from file
saved_cookies = cookielib.MozillaCookieJar('cookie_file_name')
saved_cookies.load()

opener = urllib2.build_opener() # your opener director

# do something...
opener.add_handler(urllib2.HTTPCookieProcessor(saved_cookies))

编辑:根据Python cookielib document,旧的cookie会被保留,除非被新加载的cookie覆盖。