importurllib2importcookielib#自动记录cookiecj=cookielib.CookieJar()opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))request=urllib2.Request('http://www.pythonchallenge.com/pc/def/linkedlist.php?busynothing=12345')response=opener.open(request)#获取服务器返回的cookiecookies=cj.make_cookies(response,request)# extract all cookies that this request-response pair added to the jar#修改cookiecookie=cookies[0]cookie.value='the flowers are on their way'# 将其值设为要发送的,不必自己转码request=urllib2.Request('http://www.pythonchallenge.com/pc/stuff/violin.php')cj.set_cookie(cookie)cj.add_cookie_header(request)#将cookies放入请求头部printurllib2.urlopen(request).read()