{"id":1375,"date":"2014-12-05T11:21:42","date_gmt":"2014-12-05T03:21:42","guid":{"rendered":"http:\/\/boweihe.me\/?p=1375"},"modified":"2014-12-05T11:21:42","modified_gmt":"2014-12-05T03:21:42","slug":"%e8%bd%ac%e8%bd%bdpython%e4%b8%ad%e5%ae%9e%e7%8e%b0%e5%b8%a6cookie%e7%9a%84http%e7%9a%84post%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1375","title":{"rendered":"[\u8f6c\u8f7d]Python\u4e2d\u5b9e\u73b0\u5e26Cookie\u7684Http\u7684Post\u8bf7\u6c42"},"content":{"rendered":"<p>\u672c\u6587\u8f6c\u8f7d\u81eahttp:\/\/www.crifan.com\/python_http_post_request_with_cookie\/<\/p>\n<hr \/>\n<p>&nbsp;<br \/>\n\u5df2\u7ecf\u5b9e\u73b0\u4e86\u5982\u4f55\u83b7\u5f97\u5bf9\u5e94\u7684cookie\uff0c\u5177\u4f53\u53c2\u8003\uff1a<br \/>\n<a href=\"http:\/\/www.crifan.com\/get_cookie_from_web_response_in_python\/\" target=\"_blank\" rel=\"noopener noreferrer\">\u3010\u5df2\u89e3\u51b3\u3011Python\u4e2d\u5982\u4f55\u83b7\u5f97\u8bbf\u95ee\u7f51\u9875\u6240\u8fd4\u56de\u7684cookie<\/a><br \/>\n\u73b0\u5728\u60f3\u8981\u628a\u5df2\u83b7\u5f97cookie\uff0c\u5728http\u7684\u63d0\u4ea4post\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u4e5f\u540c\u65f6\u53d1\u9001\u8fc7\u53bb\u3002<br \/>\n\u5373\uff0c\u5b9e\u73b0\u5e26cookie\u7684http\u7684post\u3002<br \/>\n\u3010\u89e3\u51b3\u8fc7\u7a0b\u3011<br \/>\n1.\u770b\u4e86\u8fd9\u91cc<br \/>\n<a href=\"http:\/\/www.ideawu.net\/blog\/archives\/270.html\">http:\/\/www.ideawu.net\/blog\/archives\/270.html<\/a><br \/>\n\u7684\u4ecb\u7ecd\uff0c\u597d\u50cf\u662fcookiejar\uff0c\u81ea\u52a8\u7ba1\u7406\u7684\uff0c\u4e0d\u9700\u8981\u624b\u52a8\u6307\u5b9a\u4e4b\u524d\u5df2\u6709\u7684cookie\u4e86\u3002<br \/>\n\u53bb\u5199\u4ee3\u7801\u8bd5\u8bd5\u3002<br \/>\n2.\u540e\u6765\u7684\u7ed3\u679c\u8bc1\u5b9e\u4e86\uff0c\u63a5\u4e0b\u6765\uff0c\u76f4\u63a5\u8c03\u7528urllib2.Request,\u5176\u81ea\u52a8\u4f1a\u628acookie\u9001\u8fc7\u53bb\u7684\uff0c<br \/>\n\u56e0\u4e3a\u4e4b\u524d\u5df2\u7ecf\u662furllib2.build_opener\uff0c\u6dfb\u52a0\u4e86\u5bf9\u5e94cookiejar\u53bb\u81ea\u52a8\u7ba1\u7406cookie\u4e86\u3002<br \/>\n\u5bf9\u5e94\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<div>\n<div id=\"highlighter_737944\" class=\"syntaxhighlighter  py \">\n<pre class=\"lang:python decode:true \"># first url request\nbaiduSpaceEntryUrl = \"http:\/\/hi.baidu.com\/motionhouse\";\ncj = cookielib.CookieJar();\nopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj));\nurllib2.install_opener(opener);\nresp = urllib2.urlopen(baiduSpaceEntryUrl);\n# second time do url request, the cookiejar will auto handle the cookie\nloginBaiduUrl = \"https:\/\/passport.baidu.com\/?login\";\npara = {\n    'username'  : username,\n    'password'  : password,\n    'mem_pass'  : 'on',\n    };\npostData = urllib.urlencode(para);\nreq = urllib2.Request(loginBaiduUrl, postData); # urllib2.Request: the HTTP request will be a POST instead of a GET when the data parameter is provided.\nreq.add_header('User-Agent', gConst['userAgentIE9']);\nreq.add_header('Content-Type', 'application\/x-www-form-urlencoded');\nreq.add_header('Cache-Control', 'no-cache');\nreq.add_header('Accept', '*\/*');\nreq.add_header('Connection', 'Keep-Alive');\nresp = urllib2.urlopen(req);\nrespInfo = resp.info();<\/pre>\n<p>&nbsp;\n<\/p><\/div>\n<\/div>\n<p>\u3010\u603b\u7ed3\u3011<br \/>\nPython\u4e2d\u7684cookiejar\uff0c\u81ea\u52a8\u5e2e\u6211\u4eec\u7ba1\u7406\u597dcookie\uff0c\u7528\u8d77\u6765\uff0c\u8fd8\u662f\u86ee\u65b9\u4fbf\u7684\u3002<br \/>\n\u6211\u4eec\u8981\u505a\u7684\u53ea\u662f\uff0c\u7b2c\u4e00\u6b21build_opener\u7684\u65f6\u5019\uff0c\u628a\u5bf9\u5e94\u7684cookiejar\u4f20\u8fdb\u53bb\uff0c<br \/>\n\u4ee5\u540e\u6bcf\u6b21\u7684http\u7684request\uff0c\u90fd\u4f1a\u81ea\u52a8\u5305\u542b\u4e86cookie\uff0c\u800c\u4e0d\u9700\u8981\u6211\u4eec\u64cd\u5fc3\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u8f6c\u8f7d\u81eahttp:\/\/www.crifan.com\/python_http_post_request_with_cookie\/ &nbsp; \u5df2\u7ecf\u5b9e\u73b0\u4e86\u5982\u4f55\u83b7\u5f97\u5bf9\u5e94\u7684cookie\uff0c\u5177\u4f53\u53c2\u8003\uff1a \u3010\u5df2\u89e3\u51b3\u3011Python\u4e2d\u5982\u4f55\u83b7\u5f97\u8bbf\u95ee\u7f51\u9875\u6240\u8fd4\u56de\u7684cookie \u73b0\u5728\u60f3\u8981\u628a\u5df2\u83b7\u5f97cookie\uff0c\u5728http\u7684\u63d0\u4ea4post\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u4e5f\u540c\u65f6\u53d1\u9001\u8fc7\u53bb\u3002 \u5373\uff0c\u5b9e\u73b0\u5e26cookie\u7684http\u7684post\u3002 \u3010\u89e3\u51b3\u8fc7\u7a0b\u3011 1.\u770b\u4e86\u8fd9\u91cc http:\/\/www.ideawu.net\/blog\/archives\/270.html \u7684\u4ecb\u7ecd\uff0c\u597d\u50cf\u662fcookiejar\uff0c\u81ea\u52a8\u7ba1\u7406\u7684\uff0c\u4e0d\u9700\u8981\u624b\u52a8\u6307\u5b9a\u4e4b\u524d\u5df2\u6709\u7684cookie\u4e86\u3002 \u53bb\u5199\u4ee3\u7801\u8bd5\u8bd5\u3002 2.\u540e\u6765\u7684\u7ed3\u679c\u8bc1\u5b9e\u4e86\uff0c\u63a5\u4e0b\u6765\uff0c\u76f4\u63a5\u8c03\u7528urllib2.Request,\u5176\u81ea\u52a8\u4f1a\u628acookie\u9001\u8fc7\u53bb\u7684\uff0c \u56e0\u4e3a\u4e4b\u524d\u5df2\u7ecf\u662furllib2.build_opener\uff0c\u6dfb\u52a0\u4e86\u5bf9\u5e94cookiejar\u53bb\u81ea\u52a8\u7ba1\u7406cookie\u4e86\u3002 \u5bf9\u5e94\u7684\u4ee3\u7801\u5982\u4e0b\uff1a # first url request baiduSpaceEntryUrl = &#8220;http:\/\/hi.baidu.com\/motionhouse&#8221;; cj = cookielib.CookieJar(); opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)); urllib2.install_opener(opener); resp = urllib2.urlopen(baiduSpaceEntryUrl); # second time do url request, the cookiejar will auto handle the cookie loginBaiduUrl = &#8220;https:\/\/passport.baidu.com\/?login&#8221;; para = { &#8216;username&#8217; : username, &#8216;password&#8217; : [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[54,89,179],"class_list":["post-1375","post","type-post","status-publish","format-standard","hentry","category-study","tag-http","tag-python","tag-179"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1375"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1375\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}