2009-03-03 30 views

回答

22
 

>>> from mechanize import Browser 
>>> b = Browser() 
>>> b._ua_handlers['_cookies'].cookiejar 
mechanize._clientcookie.CookieJar[] 
>>> b.open('http://google.com') 
response_seek_wrapper at 0xb7a922ccL whose wrapped object = closeable_response at 0xb7aa070cL whose fp = socket._fileobject object at 0xb7a94224 
>>> 
>>> b._ua_handlers['_cookies'].cookiejar 
mechanize._clientcookie.CookieJar[Cookie(version=0, name='PREF', value='ID=57d545c229b4cf3f:TM=1236081634:LM=1236081634:S=p001WJMOr-V8Rlvi', port=None, port_specified=False, domain='.google.com', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1299153634, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='PREF', value='ID=20534d80a5ccf2ea:TM=1236081635:LM=1236081635:S=jW3UotZ0dg8sv6mf', port=None, port_specified=False, domain='.google.com.ua', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1299153635, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False)] 
>>>       
 
1

Mykolas的回答幾乎給了我我正在尋找的東西。我正在尋找如何將cookie保存到文件。由於這個答案給cookie提供了保證,可以將cookie轉儲到一個文件中,也許這對其他人來這裏尋找這個有用。到cookie保存到一個文件:

br._ua_handlers['_cookies'].cookiejar.save("cookie.txt", ignore_discard=True, ignore_expires=True) 
+1

CookieJar實例具有「保存」 – Cmag 2014-07-10 02:35:07

4

只需打印CookieJar實例

# Browser 
br = mechanize.Browser() 

# Cookie Jar 
cj = cookielib.LWPCookieJar() 
br.set_cookiejar(cj) 

# Dump 
print cj 
+0

你的問題是沒有屬性?你面臨什麼問題? – 2014-12-23 18:48:34