how do I insert a cookie into a urllib2 openerdirector object?
Is there a way to append 开发者_JAVA百科a cookie into an already made and used openerdirector object?
Try this:
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))
EDIT: According to Python cookielib document, old cookies are kept unless overwritten by newly loaded ones.
精彩评论