{"id":204752,"date":"2025-05-29T09:52:48","date_gmt":"2025-05-29T01:52:48","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204752\/"},"modified":"2025-05-29T09:52:48","modified_gmt":"2025-05-29T01:52:48","slug":"python-%e8%af%b7%e6%b1%82%e5%ba%93%e5%88%9b%e5%bb%ba-cookies-%e5%af%b9%e8%b1%a1%e6%8a%a5%e9%94%99%ef%bc%9a%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3-attributeerror-nonetype-object-has-no-attribute","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204752\/","title":{"rendered":"Python \u8bf7\u6c42\u5e93\u521b\u5efa Cookies \u5bf9\u8c61\u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3 &#8220;AttributeError: &#8216;NoneType&#8217; object has no attribute &#8216;save'&#8221;\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u8bf7\u6c42\u5e93\u521b\u5efa Cookies \u5bf9\u8c61\u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3 &#8220;AttributeError: &#8216;NoneType&#8217; object has no attribute &#8216;save'&#8221;\uff1f<\/h1>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241115\/17316459446736d1f876c60.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u8bf7\u6c42\u5e93\u521b\u5efacookies\u5bf9\u8c61\u62a5\u9519<\/strong><\/p>\n<p>\u5728\u4f7f\u7528python\u8bf7\u6c42\u5e93\u521b\u5efacookies\u5bf9\u8c61\u65f6\uff0c\u6709\u65f6\u4f1a\u51fa\u73b0\u7531\u4e8e\u7cfb\u7edf\u4e0d\u77e5\u9053filename\u7684\u9519\u8bef\u3002<\/p>\n<p>\u9519\u8bef\u4fe1\u606f\u901a\u5e38\u7c7b\u4f3c\u4e8e\uff1a<\/p>\n<pre>attributeerror: 'nonetype' object has no attribute 'save'<\/pre>\n<p>\u800c\u9519\u8bef\u7684\u6839\u6e90\u5728\u4e8e\uff1a<\/p>\n<ol>\n<li><strong>\u4ee3\u7801\u9519\u8bef\uff1a<\/strong>\u672a\u6b63\u786e\u8bbe\u7f6efilename\u53c2\u6570<\/li>\n<li><strong>\u4f7f\u7528\u793a\u4f8b\u9519\u8bef\uff1a<\/strong>\u672a\u6309\u7167\u6b63\u786e\u65b9\u5f0f\u4f7f\u7528cookies\u5bf9\u8c61<\/li>\n<li><strong>requests\u4f1a\u8bdd\u7ba1\u7406\u5668\u9519\u8bef\uff1a<\/strong>\u672a\u6b63\u786e\u521d\u59cb\u5316requests\u4f1a\u8bdd\u7ba1\u7406\u5668<\/li>\n<\/ol>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<ol>\n<li><strong>\u68c0\u67e5\u4ee3\u7801\uff1a<\/strong>\u786e\u4fdd\u6b63\u786e\u8bbe\u7f6e\u4e86filename\u53c2\u6570\uff0c\u5e76\u4e14\u8be5\u53c2\u6570\u6307\u5411\u4e00\u4e2a\u6709\u6548\u7684\u6587\u4ef6\u8def\u5f84\u3002<\/li>\n<li><strong>\u4f7f\u7528\u4f1a\u8bdd\u7ba1\u7406\u5668\uff1a<\/strong>\u4f7f\u7528requests\u4f1a\u8bdd\u7ba1\u7406\u5668\u7ba1\u7406cookies\u5bf9\u8c61\u3002\u4f1a\u8bdd\u7ba1\u7406\u5668\u4f1a\u81ea\u52a8\u5904\u7406cookies\u7684\u6301\u4e45\u5316\u548c\u7ba1\u7406\u3002<\/li>\n<li><strong>\u6b63\u786e\u4f7f\u7528\u4f1a\u8bdd\u7ba1\u7406\u5668\uff1a<\/strong>\u4f7f\u7528\u4f1a\u8bdd\u7ba1\u7406\u5668\u65f6\uff0c\u76f4\u63a5\u901a\u8fc7\u4f1a\u8bdd\u7ba1\u7406\u5668\u6765\u53d1\u9001\u8bf7\u6c42\uff0c\u800c\u4e0d\u662f\u624b\u52a8\u521b\u5efa\u548c\u7ba1\u7406cookies\u5bf9\u8c61\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4fee\u590d\u9519\u8bef\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>from requests import Session\n\n# \u521b\u5efa\u4f1a\u8bdd\u7ba1\u7406\u5668\nsession = Session()\n\n# \u4f7f\u7528\u4f1a\u8bdd\u7ba1\u7406\u5668\u53d1\u9001\u8bf7\u6c42\nurl = 'https:\/\/example.com\/login'\ndata = {'username': 'username', 'password': 'password'}\nsession.post(url, data=data)\n\n# \u540e\u7eed\u8bf7\u6c42\u4f1a\u81ea\u52a8\u643a\u5e26\u767b\u5f55\u65f6\u83b7\u5f97\u7684Cookies\nres = session.get('https:\/\/example.com\/dashboard')<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528\u4f1a\u8bdd\u7ba1\u7406\u5668\uff0c\u60a8\u65e0\u9700\u624b\u52a8\u7ba1\u7406cookies\u5bf9\u8c61\uff0c\u8bf7\u6c42\u5e93\u4f1a\u81ea\u52a8\u5904\u7406cookies\u7684\u6301\u4e45\u5316\u548c\u7ba1\u7406\uff0c\u4ece\u800c\u907f\u514d\u9519\u8bef\u7684\u53d1\u751f\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aPython \u8bf7\u6c42\u5e93\u521b\u5efa Cookies \u5bf9\u8c61\u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3 &#8220;AttributeError: &#8216;NoneType&#8217; object has no attribute &#8216;save'&#8221;\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u8bf7\u6c42\u5e93\u521b\u5efa Coo&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204752","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204752"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204752\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}