{"id":1535,"date":"2014-10-24T16:06:40","date_gmt":"2014-10-24T16:06:40","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1535"},"modified":"2014-10-24T16:06:40","modified_gmt":"2014-10-24T16:06:40","slug":"python%e7%9a%84%e6%a8%a1%e6%8b%9f%e7%99%bb%e5%bd%95_tips","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1535.html","title":{"rendered":"Python\u7684\u6a21\u62df\u767b\u5f55_tips"},"content":{"rendered":"<p><strong>Python<\/strong><strong>\u7684<\/strong><strong>urllib2\u3001cookielib<\/strong><strong>\u6a21\u5757<\/strong><\/p>\n<ul>\n<li><strong>\u6ca1\u6709\u9a8c\u8bc1\u7801\u7684\u60c5\u51b5<\/strong><\/li>\n<\/ul>\n<p>\u6bd4\u5982\u767b\u5f55\u4eba\u4eba\u7f51\uff0c\u5728\u524d\u51e0\u6b21\u7684\u65f6\u5019\u4e0d\u9700\u8981\u8f93\u5165\u9a8c\u8bc1\u7801\uff0c\u53ef\u4ee5\u5148\u5c06\u7528\u6237\u540d\u3001\u5bc6\u7801\u8fdb\u884curllib.urlencode\u7f16\u7801\uff0c\u7528cookielib.CookieJar()\u751f\u6210cookie\uff0c\u7136\u540e\u751f\u6210opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))\uff0c\u4e4b\u540e\u767b\u9646\u9875\u9762req = opener.open(login_page, post_data)\u5c31\u53ef\u4ee5\u4e86\uff1b\u5b9e\u9645\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">import urllib, urllib2, cookielib, re\ndef login_func():\n    login_page = \"http:\/\/www.renren.com\/ajaxLogin\/login\"\n    data = {'email': 'your_email', 'password': 'your_password'}\n    post_data = urllib.urlencode(data)\n    cj = cookielib.CookieJar()\n    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))\n    urllib2.install_opener(opener)\n    print u\"\u767b\u5f55\u4eba\u4eba\u7f51\"\n    req = opener.open(login_page, post_data)\n    req = urllib2.urlopen(\"http:\/\/www.renren.com\/home\")\n    html = req.read()\n    uid = re.search(\"'ruid':'(d+)'\", html).group(1)\n    print  u\"\u767b\u9646\u6210\u529f\"\n    return uid<\/pre>\n<pre class=\"lang:default decode:true\">import urllib2\nimport urllib\nimport cookielib\n\ndata = {\"email\":\"your_mail\", \"password\":\"your_passwd\"}\npost_data = urllib.urlencode(data)\ncj = cookielib.CookieJar()\nopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))\nreq = urllib2.Request(\"http:\/\/www.renren.com\/PLogin.do\", post_data)\ncontent = opener.open(req)<\/pre>\n<p>&nbsp;<\/p>\n<ul>\n<li><strong>\u5982\u4f55\u7ed5\u8fc7\u9a8c\u8bc1\u7801<\/strong><\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u5728\u624b\u52a8\u767b\u5f55\u4e86\u4e4b\u540e\u7528\u63a7\u5236\u53f0\u6216Fiddler\u6293\u53d6cookie\u4fe1\u606f\uff0c\u7136\u540e\u6dfb\u52a0\u81f3header\uff08\u67092\u79cd\u65b9\u5f0f\uff09\uff1a<\/p>\n<p>Use the <em>headers<\/em> argument to the <a href=\"https:\/\/docs.python.org\/2\/library\/urllib2.html#urllib2.Request\"><strong>Request<\/strong><\/a> constructor, or:<\/p>\n<pre class=\"lang:default decode:true\">import urllib2\nreq = urllib2.Request('http:\/\/www.example.com\/')\nreq.add_header('Referer', 'http:\/\/www.python.org\/')\nr = urllib2.urlopen(req)<\/pre>\n<p><a href=\"https:\/\/docs.python.org\/2\/library\/urllib2.html#urllib2.OpenerDirector\"><strong>OpenerDirector<\/strong><\/a> automatically adds a <em>User-Agent<\/em> header to every <a href=\"https:\/\/docs.python.org\/2\/library\/urllib2.html#urllib2.Request\"><strong>Request<\/strong><\/a>. To change this:<\/p>\n<pre class=\"lang:default decode:true\">import urllib2\nopener = urllib2.build_opener()\nopener.addheaders = [('User-agent', 'Mozilla\/5.0')]\nopener.open('http:\/\/www.example.com\/')<\/pre>\n<p>Also, remember that a few standard headers (<em>Content-Length<\/em>, <em>Content-Type<\/em> and <em>Host<\/em>) are added when the <a href=\"https:\/\/docs.python.org\/2\/library\/urllib2.html#urllib2.Request\"><strong>Request<\/strong><\/a> is passed to <a href=\"https:\/\/docs.python.org\/2\/library\/urllib2.html#urllib2.urlopen\"><strong>urlopen()<\/strong><\/a> (or <a href=\"https:\/\/docs.python.org\/2\/library\/urllib2.html#urllib2.OpenerDirector.open\"><strong>OpenerDirector.open()<\/strong><\/a>).<\/p>\n<p>\u540e\u9762\u7684\u64cd\u4f5c\u5c31\u548c\u5e73\u65f6\u5dee\u4e0d\u591a\u4e86\u3002<\/p>\n<h6>\u53c2\u8003\uff1a<\/h6>\n<ul>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/urllib2.html#examples\" target=\"_blank\">20.6. urllib2 \u2014 extensible library for opening URLs \u2014 Python 2.7.8 documentation<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/3334809\/python-urllib2-how-to-send-cookie-with-urlopen-request\" target=\"_blank\">python: urllib2 how to send cookie with urlopen request &#8211; Stack Overflow<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h5><strong>\u5176\u5b83<\/strong><\/h5>\n<ul>\n<li><a href=\"http:\/\/cn.bing.com\/search?q=python+%E5%B7%B2%E6%9C%89cookie+%E7%99%BB%E5%BD%95\" target=\"_blank\">http:\/\/cn.bing.com\/search?q=python+%E5%B7%B2%E6%9C%89cookie+%E7%99%BB%E5%BD%95<\/a><\/li>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?q=Python+existing+cookie+login+web\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=Python+existing+cookie+login+web<\/a><\/li>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?q=Python+cookieJar\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=Python+cookieJar<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/urllib2.html\" target=\"_blank\">https:\/\/docs.python.org\/2\/library\/urllib2.html<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/cookielib.html\" target=\"_blank\">https:\/\/docs.python.org\/2\/library\/cookielib.html<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/6878418\/putting-a-cookie-in-a-cookiejar\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/6878418\/putting-a-cookie-in-a-cookiejar<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/2169281\/how-to-add-cookie-to-existing-cookielib-cookiejar-instance-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/2169281\/how-to-add-cookie-to-existing-cookielib-cookiejar-instance-in-python<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h6><strong>\u603b\u7ed3\u4e0b\u6765\u5c31\u662f\uff1a<\/strong><\/h6>\n<p>\u56e0\u4e3acookie\u5c31\u662fHTTP Header\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u76f4\u63a5\u6dfb\u52a0\u5230Header\u4e2d\u5c31\u884c\uff0c\u548c\u6dfb\u52a0UserAgent\u4e00\u6837\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7684urllib2\u3001cookielib\u6a21\u5757 \u6ca1\u6709\u9a8c\u8bc1\u7801\u7684\u60c5\u51b5 \u6bd4\u5982\u767b\u5f55\u4eba\u4eba\u7f51\uff0c\u5728\u524d\u51e0\u6b21\u7684\u65f6\u5019\u4e0d\u9700\u8981\u8f93 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[382,8,198],"class_list":["post-1535","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-cookielib","tag-python","tag-urllib2"],"views":4842,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=1535"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1535\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}