开发者

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜