{"id":245,"date":"2021-08-01T08:36:00","date_gmt":"2021-08-01T08:36:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=245"},"modified":"2021-08-01T08:36:00","modified_gmt":"2021-08-01T08:36:00","slug":"245","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/08\/01\/245\/","title":{"rendered":"Python\u6700\u5e38\u7528\u6293\u7f51\u9875\u5e93-Requests: \u8ba9 HTTP \u670d\u52a1\u4eba\u7c7b"},"content":{"rendered":"<p><img src=<code>https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/1287937554.png<\/code> width = <code>300<\/code> align=center \/><\/p>\n<h3>Requests \u552f\u4e00\u7684\u4e00\u4e2a\u975e\u8f6c\u57fa\u56e0\u7684 Python HTTP \u5e93\uff0c\u4eba\u7c7b\u53ef\u4ee5\u5b89\u5168\u4eab\u7528\u3002<\/h3>\n<p>\u8b66\u544a\uff1a\u975e\u4e13\u4e1a\u4f7f\u7528\u5176\u4ed6 HTTP \u5e93\u4f1a\u5bfc\u81f4\u5371\u9669\u7684\u526f\u4f5c\u7528\uff0c\u5305\u62ec\uff1a\u5b89\u5168\u7f3a\u9677\u75c7\u3001\u5197\u4f59\u4ee3\u7801\u75c7\u3001\u91cd\u65b0\u53d1\u660e\u8f6e\u5b50\u75c7\u3001\u5543\u6587\u6863\u75c7\u3001\u6291\u90c1\u3001\u5934\u75bc\u3001\u751a\u81f3\u6b7b\u4ea1\u3002<\/p>\n<h2>\u770b\u5427\uff0c\u8fd9\u5c31\u662f Requests \u7684\u5a01\u529b\uff1a<\/h2>\n<pre><code>&gt;&gt;&gt; r = requests.get('https:\/\/api.github.com\/user', auth=('user', 'pass'))\n&gt;&gt;&gt; r.status_code\n200\n&gt;&gt;&gt; r.headers['content-type']\n'application\/json; charset=utf8'\n&gt;&gt;&gt; r.encoding\n'utf-8'\n&gt;&gt;&gt; r.text\nu'{\"type\":\"User\"...'\n&gt;&gt;&gt; r.json()\n{u'private_gists': 419, u'total_private_repos': 77, ...}<\/code><\/pre>\n<p>Requests \u5141\u8bb8\u4f60\u53d1\u9001\u7eaf\u5929\u7136\uff0c\u690d\u7269\u9972\u517b\u7684 HTTP\/1.1 \u8bf7\u6c42\uff0c\u65e0\u9700\u624b\u5de5\u52b3\u52a8\u3002\u4f60\u4e0d\u9700\u8981\u624b\u52a8\u4e3a URL \u6dfb\u52a0\u67e5\u8be2\u5b57\u4e32\uff0c\u4e5f\u4e0d\u9700\u8981\u5bf9 POST \u6570\u636e\u8fdb\u884c\u8868\u5355\u7f16\u7801\u3002Keep-alive \u548c HTTP \u8fde\u63a5\u6c60\u7684\u529f\u80fd\u662f 100% \u81ea\u52a8\u5316\u7684\uff0c\u4e00\u5207\u52a8\u529b\u90fd\u6765\u81ea\u4e8e\u6839\u690d\u5728 Requests \u5185\u90e8\u7684 urllib3\u3002<\/p>\n<h3>\u7528\u6237\u89c1\u8bc1<\/h3>\n<p>Twitter\u3001Spotify\u3001Microsoft\u3001Amazon\u3001Lyft\u3001BuzzFeed\u3001Reddit\u3001NSA\u3001\u5973\u738b\u6bbf\u4e0b\u7684\u653f\u5e9c\u3001Amazon\u3001Google\u3001Twilio\u3001Mozilla\u3001Heroku\u3001PayPal\u3001NPR\u3001Obama for America\u3001Transifex\u3001Native Instruments\u3001Washington Post\u3001Twitter\u3001SoundCloud\u3001Kippt\u3001Readability\u3001\u4ee5\u53ca\u82e5\u5e72\u4e0d\u613f\u516c\u5f00\u8eab\u4efd\u7684\u8054\u90a6\u653f\u5e9c\u673a\u6784\u90fd\u5728\u5185\u90e8\u4f7f\u7528\u3002<\/p>\n<h3>Armin Ronacher<\/h3>\n<p>Requests \u662f\u4e00\u4e2a\u5b8c\u7f8e\u7684\u4f8b\u5b50\uff0c\u5b83\u8bc1\u660e\u4e86\u901a\u8fc7\u6070\u5230\u597d\u5904\u7684\u62bd\u8c61\uff0cAPI \u53ef\u4ee5\u5199\u5f97\u591a\u4e48\u4f18\u7f8e\u3002<\/p>\n<h3>Matt DeBoard<\/h3>\n<p>\u6211\u8981\u60f3\u4e2a\u529e\u6cd5\uff0c\u628a @kennethreitz \u5199\u7684 Python requests \u6a21\u5757\u505a\u6210\u7eb9\u8eab\u3002\u4e00\u5b57\u4e0d\u6f0f\u3002<\/p>\n<h3>Daniel Greenfeld<\/h3>\n<p>\u611f\u8c22 @kennethreitz \u7684 Requests \u5e93\uff0c\u521a\u521a\u7528 10 \u884c\u4ee3\u7801\u70b8\u6389\u4e86 1200 \u884c\u610f\u5927\u5229\u9762\u4ee3\u7801\u3002\u4eca\u5929\u771f\u662f\u723d\u5446\u4e86\uff01<\/p>\n<h3>Kenny Meyers<\/h3>\n<p>Python HTTP: \u7591\u60d1\u4e0e\u5426\uff0c\u90fd\u53bb\u7528 Requests \u5427\u3002\u7b80\u5355\u4f18\u7f8e\uff0c\u800c\u4e14\u7b26\u5408 Python \u98ce\u683c\u3002<\/p>\n<h1>Requests\u5e93 \u7528\u6237\u6307\u5357 \u5b98\u65b9\u6587\u6863<\/h1>\n<p><a href=\"https:\/\/docs.python-requests.org\/zh_CN\/latest\/\">https:\/\/docs.python-requests.org\/zh_CN\/latest\/<\/a><\/p>\n<h1>\u5feb\u901f\u4e0a\u624b\u00b6<\/h1>\n<p>\u8feb\u4e0d\u53ca\u5f85\u4e86\u5417\uff1f\u672c\u9875\u5185\u5bb9\u4e3a\u5982\u4f55\u5165\u95e8 Requests \u63d0\u4f9b\u4e86\u5f88\u597d\u7684\u6307\u5f15\u3002\u5176\u5047\u8bbe\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86<br \/>Requests\u3002\u5982\u679c\u8fd8\u6ca1\u6709\uff0c\u53bb<a href=\"install.html#install\">\u5b89\u88c5<\/a>\u4e00\u8282\u770b\u770b\u5427\u3002<\/p>\n<p>\u9996\u5148\uff0c\u786e\u8ba4\u4e00\u4e0b\uff1a<\/p>\n<ul>\n<li>Requests <a href=\"install.html#install\">\u5df2\u5b89\u88c5<\/a><\/li>\n<li>Requests <a href=\"..\/community\/updates.html#updates\">\u662f\u6700\u65b0\u7684<\/a><\/li>\n<\/ul>\n<p>\u8ba9\u6211\u4eec\u4ece\u4e00\u4e9b\u7b80\u5355\u7684\u793a\u4f8b\u5f00\u59cb\u5427\u3002<\/p>\n<h2>\u53d1\u9001\u8bf7\u6c42\u00b6<\/h2>\n<p>\u4f7f\u7528 Requests \u53d1\u9001\u7f51\u7edc\u8bf7\u6c42\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<p>\u4e00\u5f00\u59cb\u8981\u5bfc\u5165 Requests \u6a21\u5757\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; import requests\n\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u5c1d\u8bd5\u83b7\u53d6\u67d0\u4e2a\u7f51\u9875\u3002\u672c\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6765\u83b7\u53d6 Github \u7684\u516c\u5171\u65f6\u95f4\u7ebf\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; r = requests.get('https:\/\/api.github.com\/events')\n\n<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a <code>r<\/code> \u7684 [<code>Response<\/code>] \u5bf9\u8c61\u3002\u6211\u4eec\u53ef\u4ee5\u4ece\u8fd9\u4e2a\u5bf9\u8c61\u4e2d\u83b7\u53d6\u6240\u6709\u6211\u4eec\u60f3\u8981\u7684\u4fe1\u606f\u3002<\/p>\n<p>Requests \u7b80\u4fbf\u7684 API \u610f\u5473\u7740\u6240\u6709 HTTP \u8bf7\u6c42\u7c7b\u578b\u90fd\u662f\u663e\u800c\u6613\u89c1\u7684\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u53d1\u9001\u4e00\u4e2a HTTP POST \u8bf7\u6c42\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; r = requests.post('http:\/\/httpbin.org\/post', data = {'key':'value'})\n<\/code><\/pre>\n<p>\u6f02\u4eae\uff0c\u5bf9\u5427\uff1f\u90a3\u4e48\u5176\u4ed6 HTTP \u8bf7\u6c42\u7c7b\u578b\uff1aPUT\uff0cDELETE\uff0cHEAD \u4ee5\u53ca OPTIONS \u53c8\u662f\u5982\u4f55\u7684\u5462\uff1f\u90fd\u662f\u4e00\u6837\u7684\u7b80\u5355\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; r = requests.put('http:\/\/httpbin.org\/put', data = {'key':'value'})\n&gt;&gt;&gt; r = requests.delete('http:\/\/httpbin.org\/delete')\n&gt;&gt;&gt; r = requests.head('http:\/\/httpbin.org\/get')\n&gt;&gt;&gt; r = requests.options('http:\/\/httpbin.org\/get')\n<\/code><\/pre>\n<p>\u90fd\u5f88\u4e0d\u9519\u5427\uff0c\u4f46\u8fd9\u4e5f\u4ec5\u662f Requests \u7684\u51b0\u5c71\u4e00\u89d2\u5462\u3002<\/p>\n<h2>\u4f20\u9012 URL \u53c2\u6570\u00b6<\/h2>\n<p>\u4f60\u4e5f\u8bb8\u7ecf\u5e38\u60f3\u4e3a URL \u7684\u67e5\u8be2\u5b57\u7b26\u4e32(query string)\u4f20\u9012\u67d0\u79cd\u6570\u636e\u3002\u5982\u679c\u4f60\u662f\u624b\u5de5\u6784\u5efa URL\uff0c\u90a3\u4e48\u6570\u636e\u4f1a\u4ee5\u952e\/\u503c\u5bf9\u7684\u5f62\u5f0f\u7f6e\u4e8e URL<br \/>\u4e2d\uff0c\u8ddf\u5728\u4e00\u4e2a\u95ee\u53f7\u7684\u540e\u9762\u3002\u4f8b\u5982\uff0c <code>httpbin.org\/get?key=val<\/code>\u3002 Requests \u5141\u8bb8\u4f60\u4f7f\u7528 <code>params<\/code><br \/>\u5173\u952e\u5b57\u53c2\u6570\uff0c\u4ee5\u4e00\u4e2a\u5b57\u7b26\u4e32\u5b57\u5178\u6765\u63d0\u4f9b\u8fd9\u4e9b\u53c2\u6570\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u679c\u4f60\u60f3\u4f20\u9012 <code>key1=value1<\/code> \u548c <code>key2=value2<\/code> \u5230<br \/><code>httpbin.org\/get<\/code> \uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; payload = {'key1': 'value1', 'key2': 'value2'}\n&gt;&gt;&gt; r = requests.get(\"http:\/\/httpbin.org\/get\", params=payload)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u6253\u5370\u8f93\u51fa\u8be5 URL\uff0c\u4f60\u80fd\u770b\u5230 URL \u5df2\u88ab\u6b63\u786e\u7f16\u7801\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; print(r.url)\nhttp:\/\/httpbin.org\/get?key2=value2&key1=value1\n\n<\/code><\/pre>\n<p>\u6ce8\u610f\u5b57\u5178\u91cc\u503c\u4e3a <code>None<\/code> \u7684\u952e\u90fd\u4e0d\u4f1a\u88ab\u6dfb\u52a0\u5230 URL \u7684\u67e5\u8be2\u5b57\u7b26\u4e32\u91cc\u3002<\/p>\n<p>\u4f60\u8fd8\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5217\u8868\u4f5c\u4e3a\u503c\u4f20\u5165\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; payload = {'key1': 'value1', 'key2': ['value2', 'value3']}\n\n&gt;&gt;&gt; r = requests.get('http:\/\/httpbin.org\/get', params=payload)\n&gt;&gt;&gt; print(r.url)\nhttp:\/\/httpbin.org\/get?key1=value1&key2=value2&key2=value3\n<\/code><\/pre>\n<h2>\u54cd\u5e94\u5185\u5bb9\u00b6<\/h2>\n<p>\u6211\u4eec\u80fd\u8bfb\u53d6\u670d\u52a1\u5668\u54cd\u5e94\u7684\u5185\u5bb9\u3002\u518d\u6b21\u4ee5 GitHub \u65f6\u95f4\u7ebf\u4e3a\u4f8b\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; import requests\n&gt;&gt;&gt; r = requests.get('https:\/\/api.github.com\/events')\n&gt;&gt;&gt; r.text\nu'[{\"repository\":{\"open_issues\":0,\"url\":\"https:\/\/github.com\/...\n<\/code><\/pre>\n<p>Requests \u4f1a\u81ea\u52a8\u89e3\u7801\u6765\u81ea\u670d\u52a1\u5668\u7684\u5185\u5bb9\u3002\u5927\u591a\u6570 unicode \u5b57\u7b26\u96c6\u90fd\u80fd\u88ab\u65e0\u7f1d\u5730\u89e3\u7801\u3002<\/p>\n<p>\u8bf7\u6c42\u53d1\u51fa\u540e\uff0cRequests \u4f1a\u57fa\u4e8e HTTP \u5934\u90e8\u5bf9\u54cd\u5e94\u7684\u7f16\u7801\u4f5c\u51fa\u6709\u6839\u636e\u7684\u63a8\u6d4b\u3002\u5f53\u4f60\u8bbf\u95ee <code>r.text<\/code> \u4e4b\u65f6\uff0cRequests<br \/>\u4f1a\u4f7f\u7528\u5176\u63a8\u6d4b\u7684\u6587\u672c\u7f16\u7801\u3002\u4f60\u53ef\u4ee5\u627e\u51fa Requests \u4f7f\u7528\u4e86\u4ec0\u4e48\u7f16\u7801\uff0c\u5e76\u4e14\u80fd\u591f\u4f7f\u7528 <code>r.encoding<\/code> \u5c5e\u6027\u6765\u6539\u53d8\u5b83\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; r.encoding\n'utf-8'\n&gt;&gt;&gt; r.encoding = 'ISO-8859-1'\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u6539\u53d8\u4e86\u7f16\u7801\uff0c\u6bcf\u5f53\u4f60\u8bbf\u95ee <code>r.text<\/code> \uff0cRequest \u90fd\u5c06\u4f1a\u4f7f\u7528 <code>r.encoding<\/code><br \/>\u7684\u65b0\u503c\u3002\u4f60\u53ef\u80fd\u5e0c\u671b\u5728\u4f7f\u7528\u7279\u6b8a\u903b\u8f91\u8ba1\u7b97\u51fa\u6587\u672c\u7684\u7f16\u7801\u7684\u60c5\u51b5\u4e0b\u6765\u4fee\u6539\u7f16\u7801\u3002\u6bd4\u5982 HTTP \u548c XML \u81ea\u8eab\u53ef\u4ee5\u6307\u5b9a\u7f16\u7801\u3002\u8fd9\u6837\u7684\u8bdd\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528<br \/><code>r.content<\/code> \u6765\u627e\u5230\u7f16\u7801\uff0c\u7136\u540e\u8bbe\u7f6e <code>r.encoding<\/code> \u4e3a\u76f8\u5e94\u7684\u7f16\u7801\u3002\u8fd9\u6837\u5c31\u80fd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u89e3\u6790 <code>r.text<\/code> \u4e86\u3002<\/p>\n<p>\u5728\u4f60\u9700\u8981\u7684\u60c5\u51b5\u4e0b\uff0cRequests \u4e5f\u53ef\u4ee5\u4f7f\u7528\u5b9a\u5236\u7684\u7f16\u7801\u3002\u5982\u679c\u4f60\u521b\u5efa\u4e86\u81ea\u5df1\u7684\u7f16\u7801\uff0c\u5e76\u4f7f\u7528 <code>codecs<\/code><br \/>\u6a21\u5757\u8fdb\u884c\u6ce8\u518c\uff0c\u4f60\u5c31\u53ef\u4ee5\u8f7b\u677e\u5730\u4f7f\u7528\u8fd9\u4e2a\u89e3\u7801\u5668\u540d\u79f0\u4f5c\u4e3a <code>r.encoding<\/code> \u7684\u503c\uff0c \u7136\u540e\u7531 Requests \u6765\u4e3a\u4f60\u5904\u7406\u7f16\u7801\u3002<\/p>\n<h2>\u4e8c\u8fdb\u5236\u54cd\u5e94\u5185\u5bb9\u00b6<\/h2>\n<p>\u4f60\u4e5f\u80fd\u4ee5\u5b57\u8282\u7684\u65b9\u5f0f\u8bbf\u95ee\u8bf7\u6c42\u54cd\u5e94\u4f53\uff0c\u5bf9\u4e8e\u975e\u6587\u672c\u8bf7\u6c42\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; r.content\nb'[{\"repository\":{\"open_issues\":0,\"url\":\"https:\/\/github.com\/...\n<\/code><\/pre>\n<p>Requests \u4f1a\u81ea\u52a8\u4e3a\u4f60\u89e3\u7801 <code>gzip<\/code> \u548c <code>deflate<\/code> \u4f20\u8f93\u7f16\u7801\u7684\u54cd\u5e94\u6570\u636e\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4ee5\u8bf7\u6c42\u8fd4\u56de\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u521b\u5efa\u4e00\u5f20\u56fe\u7247\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; from PIL import Image\n&gt;&gt;&gt; from io import BytesIO\n\n&gt;&gt;&gt; i = Image.open(BytesIO(r.content))\n<\/code><\/pre>\n<h2>JSON \u54cd\u5e94\u5185\u5bb9\u00b6<\/h2>\n<p>Requests \u4e2d\u4e5f\u6709\u4e00\u4e2a\u5185\u7f6e\u7684 JSON \u89e3\u7801\u5668\uff0c\u52a9\u4f60\u5904\u7406 JSON \u6570\u636e\uff1a<\/p>\n<pre><code>\n&gt;&gt;&gt; import requests\n\n&gt;&gt;&gt; r = requests.get('https:\/\/api.github.com\/events')\n&gt;&gt;&gt; r.json()\n[{u'repository': {u'open_issues': 0, u'url': 'https:\/\/github.com\/...\n\n<\/code><\/pre>\n<p>\u5982\u679c JSON \u89e3\u7801\u5931\u8d25\uff0c <code>r.json()<\/code> \u5c31\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u54cd\u5e94\u5185\u5bb9\u662f 401 (Unauthorized)\uff0c\u5c1d\u8bd5\u8bbf\u95ee <code>r.json()<\/code><br \/>\u5c06\u4f1a\u629b\u51fa <code>ValueError: No JSON object could be decoded<\/code> \u5f02\u5e38\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6210\u529f\u8c03\u7528 <code>r.json()<\/code> \u5e76<strong>\u4e0d<\/strong>\u610f\u5473\u7740\u54cd\u5e94\u7684\u6210\u529f\u3002\u6709\u7684\u670d\u52a1\u5668\u4f1a\u5728\u5931\u8d25\u7684\u54cd\u5e94\u4e2d\u5305\u542b\u4e00\u4e2a JSON \u5bf9\u8c61\uff08\u6bd4\u5982 HTTP 500<br \/>\u7684\u9519\u8bef\u7ec6\u8282\uff09\u3002\u8fd9\u79cd JSON \u4f1a\u88ab\u89e3\u7801\u8fd4\u56de\u3002\u8981\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f\uff0c\u8bf7\u4f7f\u7528 <code>r.raise_for_status()<\/code> \u6216\u8005\u68c0\u67e5<br \/><code>r.status_code<\/code> \u662f\u5426\u548c\u4f60\u7684\u671f\u671b\u76f8\u540c\u3002<\/p>\n<h2>\u539f\u59cb\u54cd\u5e94\u5185\u5bb9\u00b6<\/h2>\n<p>\u5728\u7f55\u89c1\u7684\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u60f3\u83b7\u53d6\u6765\u81ea\u670d\u52a1\u5668\u7684\u539f\u59cb\u5957\u63a5\u5b57\u54cd\u5e94\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u8bbf\u95ee <code>r.raw<\/code>\u3002 \u5982\u679c\u4f60\u786e\u5b9e\u60f3\u8fd9\u4e48\u5e72\uff0c\u90a3\u8bf7\u4f60\u786e\u4fdd\u5728\u521d\u59cb\u8bf7\u6c42\u4e2d\u8bbe\u7f6e\u4e86<br \/><code>stream=True<\/code>\u3002\u5177\u4f53\u4f60\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; r = requests.get('https:\/\/api.github.com\/events', stream=True)\n&gt;&gt;&gt; r.raw\n&lt;requests.packages.urllib3.response.HTTPResponse object at 0x101194810&gt;\n&gt;&gt;&gt; r.raw.read(10)\n'x1fx8bx08x00x00x00x00x00x00x03'\n\n<\/code><\/pre>\n<p>\u4f46\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u4f60\u5e94\u8be5\u4ee5\u4e0b\u9762\u7684\u6a21\u5f0f\u5c06\u6587\u672c\u6d41\u4fdd\u5b58\u5230\u6587\u4ef6\uff1a<\/p>\n<pre><code>with open(filename, 'wb') as fd:\n    for chunk in r.iter_content(chunk_size):\n        fd.write(chunk)\n<\/code><\/pre>\n<p>\u4f7f\u7528 <code>Response.iter_content<\/code> \u5c06\u4f1a\u5904\u7406\u5927\u91cf\u4f60\u76f4\u63a5\u4f7f\u7528 <code>Response.raw<\/code> \u4e0d\u5f97\u4e0d\u5904\u7406\u7684\u3002<br \/>\u5f53\u6d41\u4e0b\u8f7d\u65f6\uff0c\u4e0a\u9762\u662f\u4f18\u5148\u63a8\u8350\u7684\u83b7\u53d6\u5185\u5bb9\u65b9\u5f0f\u3002 Note that <code>chunk_size<\/code> can be freely adjusted to a<br \/>number that may better fit your use cases.<\/p>\n<h2>\u5b9a\u5236\u8bf7\u6c42\u5934\u00b6<\/h2>\n<p>\u5982\u679c\u4f60\u60f3\u4e3a\u8bf7\u6c42\u6dfb\u52a0 HTTP \u5934\u90e8\uff0c\u53ea\u8981\u7b80\u5355\u5730\u4f20\u9012\u4e00\u4e2a <code>dict<\/code> \u7ed9 <code>headers<\/code> \u53c2\u6570\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5728\u524d\u4e00\u4e2a\u793a\u4f8b\u4e2d\u6211\u4eec\u6ca1\u6709\u6307\u5b9a content-type:<\/p>\n<pre><code>&gt;&gt;&gt; url = 'https:\/\/api.github.com\/some\/endpoint'\n&gt;&gt;&gt; headers = {'user-agent': 'my-app\/0.0.1'}\n\n&gt;&gt;&gt; r = requests.get(url, headers=headers)\n<\/code><\/pre>\n<p>\u6ce8\u610f: \u5b9a\u5236 header \u7684\u4f18\u5148\u7ea7\u4f4e\u4e8e\u67d0\u4e9b\u7279\u5b9a\u7684\u4fe1\u606f\u6e90\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u5982\u679c\u5728 <code>.netrc<\/code> \u4e2d\u8bbe\u7f6e\u4e86\u7528\u6237\u8ba4\u8bc1\u4fe1\u606f\uff0c\u4f7f\u7528 headers= \u8bbe\u7f6e\u7684\u6388\u6743\u5c31\u4e0d\u4f1a\u751f\u6548\u3002\u800c\u5982\u679c\u8bbe\u7f6e\u4e86 <code>auth=<\/code> \u53c2\u6570\uff0c<code>.netrc<\/code> \u7684\u8bbe\u7f6e\u5c31\u65e0\u6548\u4e86\u3002<\/li>\n<li>\u5982\u679c\u88ab\u91cd\u5b9a\u5411\u5230\u522b\u7684\u4e3b\u673a\uff0c\u6388\u6743 header \u5c31\u4f1a\u88ab\u5220\u9664\u3002<\/li>\n<li>\u4ee3\u7406\u6388\u6743 header \u4f1a\u88ab URL \u4e2d\u63d0\u4f9b\u7684\u4ee3\u7406\u8eab\u4efd\u8986\u76d6\u6389\u3002<\/li>\n<li>\u5728\u6211\u4eec\u80fd\u5224\u65ad\u5185\u5bb9\u957f\u5ea6\u7684\u60c5\u51b5\u4e0b\uff0cheader \u7684 Content-Length \u4f1a\u88ab\u6539\u5199\u3002<\/li>\n<\/ul>\n<p>\u66f4\u8fdb\u4e00\u6b65\u8bb2\uff0cRequests \u4e0d\u4f1a\u57fa\u4e8e\u5b9a\u5236 header \u7684\u5177\u4f53\u60c5\u51b5\u6539\u53d8\u81ea\u5df1\u7684\u884c\u4e3a\u3002\u53ea\u4e0d\u8fc7\u5728\u6700\u540e\u7684\u8bf7\u6c42\u4e2d\uff0c\u6240\u6709\u7684 header \u4fe1\u606f\u90fd\u4f1a\u88ab\u4f20\u9012\u8fdb\u53bb\u3002<\/p>\n<p>\u6ce8\u610f: \u6240\u6709\u7684 header \u503c\u5fc5\u987b\u662f <code>string<\/code>\u3001bytestring \u6216\u8005 unicode\u3002\u5c3d\u7ba1\u4f20\u9012 unicode header<br \/>\u4e5f\u662f\u5141\u8bb8\u7684\uff0c\u4f46\u4e0d\u5efa\u8bae\u8fd9\u6837\u505a\u3002<\/p>\n<h2>\u66f4\u52a0\u590d\u6742\u7684 POST \u8bf7\u6c42\u00b6<\/h2>\n<p>\u901a\u5e38\uff0c\u4f60\u60f3\u8981\u53d1\u9001\u4e00\u4e9b\u7f16\u7801\u4e3a\u8868\u5355\u5f62\u5f0f\u7684\u6570\u636e\u2014\u2014\u975e\u5e38\u50cf\u4e00\u4e2a HTML \u8868\u5355\u3002\u8981\u5b9e\u73b0\u8fd9\u4e2a\uff0c\u53ea\u9700\u7b80\u5355\u5730\u4f20\u9012\u4e00\u4e2a\u5b57\u5178\u7ed9 data<br \/>\u53c2\u6570\u3002\u4f60\u7684\u6570\u636e\u5b57\u5178\u5728\u53d1\u51fa\u8bf7\u6c42\u65f6\u4f1a\u81ea\u52a8\u7f16\u7801\u4e3a\u8868\u5355\u5f62\u5f0f\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; payload = {'key1': 'value1', 'key2': 'value2'}\n\n&gt;&gt;&gt; r = requests.post(\"http:\/\/httpbin.org\/post\", data=payload)\n&gt;&gt;&gt; print(r.text)\n{\n  ...\n  \"form\": {\n    \"key2\": \"value2\",\n    \"key1\": \"value1\"\n  },\n  ...\n}\n<\/code><\/pre>\n<p>\u4f60\u8fd8\u53ef\u4ee5\u4e3a <code>data<\/code> \u53c2\u6570\u4f20\u5165\u4e00\u4e2a\u5143\u7ec4\u5217\u8868\u3002\u5728\u8868\u5355\u4e2d\u591a\u4e2a\u5143\u7d20\u4f7f\u7528\u540c\u4e00 key \u7684\u65f6\u5019\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5c24\u5176\u6709\u6548\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; payload = (('key1', 'value1'), ('key1', 'value2'))\n&gt;&gt;&gt; r = requests.post('http:\/\/httpbin.org\/post', data=payload)\n&gt;&gt;&gt; print(r.text)\n{\n  ...\n  \"form\": {\n    \"key1\": [\n      \"value1\",\n      \"value2\"\n    ]\n  },\n  ...\n}\n<\/code><\/pre>\n<p>\u5f88\u591a\u65f6\u5019\u4f60\u60f3\u8981\u53d1\u9001\u7684\u6570\u636e\u5e76\u975e\u7f16\u7801\u4e3a\u8868\u5355\u5f62\u5f0f\u7684\u3002\u5982\u679c\u4f60\u4f20\u9012\u4e00\u4e2a <code>string<\/code> \u800c\u4e0d\u662f\u4e00\u4e2a <code>dict<\/code>\uff0c\u90a3\u4e48\u6570\u636e\u4f1a\u88ab\u76f4\u63a5\u53d1\u5e03\u51fa\u53bb\u3002<\/p>\n<p>\u4f8b\u5982\uff0cGithub API v3 \u63a5\u53d7\u7f16\u7801\u4e3a JSON \u7684 POST\/PATCH \u6570\u636e\uff1a<\/p>\n<pre><code>\n&gt;&gt;&gt; import json\n\n&gt;&gt;&gt; url = 'https:\/\/api.github.com\/some\/endpoint'\n&gt;&gt;&gt; payload = {'some': 'data'}\n\n&gt;&gt;&gt; r = requests.post(url, data=json.dumps(payload))\n<\/code><\/pre>\n<p>\u6b64\u5904\u9664\u4e86\u53ef\u4ee5\u81ea\u884c\u5bf9 <code>dict<\/code> \u8fdb\u884c\u7f16\u7801\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>json<\/code> \u53c2\u6570\u76f4\u63a5\u4f20\u9012\uff0c\u7136\u540e\u5b83\u5c31\u4f1a\u88ab\u81ea\u52a8\u7f16\u7801\u3002\u8fd9\u662f 2.4.2 \u7248\u7684\u65b0\u52a0\u529f\u80fd\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; url = 'https:\/\/api.github.com\/some\/endpoint'\n&gt;&gt;&gt; payload = {'some': 'data'}\n\n&gt;&gt;&gt; r = requests.post(url, json=payload)\n<\/code><\/pre>\n<h2>POST\u4e00\u4e2a\u591a\u90e8\u5206\u7f16\u7801(Multipart-Encoded)\u7684\u6587\u4ef6\u00b6<\/h2>\n<p>Requests \u4f7f\u5f97\u4e0a\u4f20\u591a\u90e8\u5206\u7f16\u7801\u6587\u4ef6\u53d8\u5f97\u5f88\u7b80\u5355\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; url = 'http:\/\/httpbin.org\/post'\n&gt;&gt;&gt; files = {'file': open('report.xls', 'rb')}\n\n&gt;&gt;&gt; r = requests.post(url, files=files)\n&gt;&gt;&gt; r.text\n{\n  ...\n  \"files\": {\n    \"file\": \"&lt;censored...binary...data&gt;\"\n  },\n  ...\n}\n<\/code><\/pre>\n<p>\u4f60\u53ef\u4ee5\u663e\u5f0f\u5730\u8bbe\u7f6e\u6587\u4ef6\u540d\uff0c\u6587\u4ef6\u7c7b\u578b\u548c\u8bf7\u6c42\u5934\uff1a<\/p>\n<pre><code>\n&gt;&gt;&gt; url = 'http:\/\/httpbin.org\/post'\n&gt;&gt;&gt; files = {'file': ('report.xls', open('report.xls', 'rb'), 'application\/vnd.ms-excel', {'Expires': '0'})}\n\n&gt;&gt;&gt; r = requests.post(url, files=files)\n&gt;&gt;&gt; r.text\n{\n  ...\n  \"files\": {\n    \"file\": \"&lt;censored...binary...data&gt;\"\n  },\n  ...\n}\n\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u60f3\uff0c\u4f60\u4e5f\u53ef\u4ee5\u53d1\u9001\u4f5c\u4e3a\u6587\u4ef6\u6765\u63a5\u6536\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; url = 'http:\/\/httpbin.org\/post'\n&gt;&gt;&gt; files = {'file': ('report.csv', 'some,data,to,send<\/code><\/pre>\n<p>another,row,to,send<br \/>')}<\/p>\n<pre><code>\n&gt;&gt;&gt; r = requests.post(url, files=files)\n&gt;&gt;&gt; r.text\n{\n  ...\n  \"files\": {\n    \"file\": \"some,data,to,send\\nanother,row,to,send\\n\"\n  },\n  ...\n}\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u53d1\u9001\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u6587\u4ef6\u4f5c\u4e3a <code>multipart\/form-data<\/code> \u8bf7\u6c42\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u5c06\u8bf7\u6c42\u505a\u6210\u6570\u636e\u6d41\u3002\u9ed8\u8ba4\u4e0b <code>requests<\/code> \u4e0d\u652f\u6301,<br \/>\u4f46\u6709\u4e2a\u7b2c\u4e09\u65b9\u5305 <code>requests-toolbelt<\/code> \u662f\u652f\u6301\u7684\u3002\u4f60\u53ef\u4ee5\u9605\u8bfb <a href=\"https:\/\/toolbelt.rtfd.org\">toolbelt\u6587\u6863<\/a> \u6765\u4e86\u89e3\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<p>\u5728\u4e00\u4e2a\u8bf7\u6c42\u4e2d\u53d1\u9001\u591a\u6587\u4ef6\u53c2\u8003 <code>\u9ad8\u7ea7\u7528\u6cd5<\/code> \u4e00\u8282\u3002<\/p>\n<p>\u8b66\u544a<\/p>\n<p>\u6211\u4eec\u5f3a\u70c8\u5efa\u8bae\u4f60\u7528\u4e8c\u8fdb\u5236\u6a21\u5f0f([binary<br \/>mode](<a href=\"https:\/\/docs.python.org\/2\/tutorial\/inputoutput.html#reading-and-writing-files\">https:\/\/docs.python.org\/2\/tutorial\/inputoutput.html#reading-and-writing-files<\/a>))\u6253\u5f00\u6587\u4ef6\u3002\u8fd9\u662f\u56e0\u4e3a Requests \u53ef\u80fd\u4f1a\u8bd5\u56fe\u4e3a\u4f60\u63d0\u4f9b <code>Content-Length<\/code><br \/>header\uff0c\u5728\u5b83\u8fd9\u6837\u505a\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u503c\u4f1a\u88ab\u8bbe\u4e3a\u6587\u4ef6\u7684\u5b57\u8282\u6570\uff08 <em>bytes<\/em> \uff09\u3002\u5982\u679c\u7528\u6587\u672c\u6a21\u5f0f(text mode)\u6253\u5f00\u6587\u4ef6\uff0c\u5c31\u53ef\u80fd\u4f1a\u53d1\u751f\u9519\u8bef\u3002<\/p>\n<h2>\u54cd\u5e94\u72b6\u6001\u7801\u00b6<\/h2>\n<p>\u6211\u4eec\u53ef\u4ee5\u68c0\u6d4b\u54cd\u5e94\u72b6\u6001\u7801\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; r = requests.get('http:\/\/httpbin.org\/get')\n&gt;&gt;&gt; r.status_code\n200\n<\/code><\/pre>\n<p>\u4e3a\u65b9\u4fbf\u5f15\u7528\uff0cRequests\u8fd8\u9644\u5e26\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684\u72b6\u6001\u7801\u67e5\u8be2\u5bf9\u8c61\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; r.status_code == requests.codes.ok\nTrue\n\n<\/code><\/pre>\n<p>\u5982\u679c\u53d1\u9001\u4e86\u4e00\u4e2a\u9519\u8bef\u8bf7\u6c42(\u4e00\u4e2a 4XX \u5ba2\u6237\u7aef\u9519\u8bef\uff0c\u6216\u8005 5XX \u670d\u52a1\u5668\u9519\u8bef\u54cd\u5e94)\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>Response.raise_for_status()<\/code> \u6765\u629b\u51fa\u5f02\u5e38\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; bad_r = requests.get('http:\/\/httpbin.org\/status\/404')\n&gt;&gt;&gt; bad_r.status_code\n404\n\n&gt;&gt;&gt; bad_r.raise_for_status()\nTraceback (most recent call last):\n  File \"requests\/models.py\", line 832, in raise_for_status\n    raise http_error\nrequests.exceptions.HTTPError: 404 Client Error\n<\/code><\/pre>\n<p>\u4f46\u662f\uff0c\u7531\u4e8e\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d <code>r<\/code> \u7684 <code>status_code<\/code> \u662f <code>200<\/code> \uff0c\u5f53\u6211\u4eec\u8c03\u7528 <code>raise_for_status()<\/code> \u65f6\uff0c\u5f97\u5230\u7684\u662f\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; r.raise_for_status()\nNone\n\n<\/code><\/pre>\n<p>\u4e00\u5207\u90fd\u633a\u548c\u8c10\u54c8\u3002<\/p>\n<h2>\u54cd\u5e94\u5934\u00b6<\/h2>\n<p>\u6211\u4eec\u53ef\u4ee5\u67e5\u770b\u4ee5\u4e00\u4e2a Python \u5b57\u5178\u5f62\u5f0f\u5c55\u793a\u7684\u670d\u52a1\u5668\u54cd\u5e94\u5934\uff1a<\/p>\n<pre><code>\n&gt;&gt;&gt; r.headers\n{\n    'content-encoding': 'gzip',\n    'transfer-encoding': 'chunked',\n    'connection': 'close',\n    'server': 'nginx\/1.0.4',\n    'x-runtime': '148ms',\n    'etag': '\"e1ca502697e5c9317743dc078f67693f\"',\n    'content-type': 'application\/json'\n}\n\n<\/code><\/pre>\n<p>\u4f46\u662f\u8fd9\u4e2a\u5b57\u5178\u6bd4\u8f83\u7279\u6b8a\uff1a\u5b83\u662f\u4ec5\u4e3a HTTP \u5934\u90e8\u800c\u751f\u7684\u3002\u6839\u636e [RFC<br \/>2616](<a href=\"http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec14.html\">http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec14.html<\/a>)\uff0c HTTP<br \/>\u5934\u90e8\u662f\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u7684\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u5927\u5199\u5f62\u5f0f\u6765\u8bbf\u95ee\u8fd9\u4e9b\u54cd\u5e94\u5934\u5b57\u6bb5\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; r.headers['Content-Type']\n'application\/json'\n\n&gt;&gt;&gt; r.headers.get('content-type')\n'application\/json'\n\n<\/code><\/pre>\n<p>\u5b83\u8fd8\u6709\u4e00\u4e2a\u7279\u6b8a\u70b9\uff0c\u90a3\u5c31\u662f\u670d\u52a1\u5668\u53ef\u4ee5\u591a\u6b21\u63a5\u53d7\u540c\u4e00 header\uff0c\u6bcf\u6b21\u90fd\u4f7f\u7528\u4e0d\u540c\u7684\u503c\u3002\u4f46 Requests<br \/>\u4f1a\u5c06\u5b83\u4eec\u5408\u5e76\uff0c\u8fd9\u6837\u5b83\u4eec\u5c31\u53ef\u4ee5\u7528\u4e00\u4e2a\u6620\u5c04\u6765\u8868\u793a\u51fa\u6765\uff0c\u53c2\u89c1 [RFC<br \/>7230](<a href=\"http:\/\/tools.ietf.org\/html\/rfc7230#section-3.2\">http:\/\/tools.ietf.org\/html\/rfc7230#section-3.2<\/a>):<\/p>\n<blockquote>\n<p>A recipient MAY combine multiple header fields with the same field name into<br \/>one <code>field-name: field-value<\/code> pair, without changing the semantics of the<br \/>message, by appending each subsequent field value to the combined field<br \/>value in order, separated by a comma.<\/p>\n<p>\u63a5\u6536\u8005\u53ef\u4ee5\u5408\u5e76\u591a\u4e2a\u76f8\u540c\u540d\u79f0\u7684 header \u680f\u4f4d\uff0c\u628a\u5b83\u4eec\u5408\u4e3a\u4e00\u4e2a <code>field-name: field-value<\/code><br \/>\u914d\u5bf9\uff0c\u5c06\u6bcf\u4e2a\u540e\u7eed\u7684\u680f\u4f4d\u503c\u4f9d\u6b21\u8ffd\u52a0\u5230\u5408\u5e76\u7684\u680f\u4f4d\u503c\u4e2d\uff0c\u7528\u9017\u53f7\u9694\u5f00\u5373\u53ef\uff0c\u8fd9\u6837\u505a\u4e0d\u4f1a\u6539\u53d8\u4fe1\u606f\u7684\u8bed\u4e49\u3002<\/p>\n<\/blockquote>\n<h2>Cookie\u00b6<\/h2>\n<p>\u5982\u679c\u67d0\u4e2a\u54cd\u5e94\u4e2d\u5305\u542b\u4e00\u4e9b cookie\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u8bbf\u95ee\u5b83\u4eec\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; url = 'http:\/\/example.com\/some\/cookie\/setting\/url'\n&gt;&gt;&gt; r = requests.get(url)\n\n&gt;&gt;&gt; r.cookies['example_cookie_name']\n'example_cookie_value'\n<\/code><\/pre>\n<p>\u8981\u60f3\u53d1\u9001\u4f60\u7684cookies\u5230\u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>cookies<\/code> \u53c2\u6570\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; url = 'http:\/\/httpbin.org\/cookies'\n&gt;&gt;&gt; cookies = dict(cookies_are='working')\n\n&gt;&gt;&gt; r = requests.get(url, cookies=cookies)\n&gt;&gt;&gt; r.text\n'{\"cookies\": {\"cookies_are\": \"working\"}}'\n<\/code><\/pre>\n<p>Cookie \u7684\u8fd4\u56de\u5bf9\u8c61\u4e3a<br \/><code>RequestsCookieJar<\/code> \uff0c\u5b83\u7684\u884c\u4e3a\u548c\u5b57\u5178\u7c7b\u4f3c\uff0c\u4f46\u63a5\u53e3\u66f4\u4e3a\u5b8c\u6574\uff0c\u9002\u5408\u8de8\u57df\u540d\u8de8\u8def\u5f84\u4f7f\u7528\u3002\u4f60\u8fd8\u53ef\u4ee5\u628a<br \/>Cookie Jar \u4f20\u5230 Requests \u4e2d\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; jar = requests.cookies.RequestsCookieJar()\n&gt;&gt;&gt; jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='\/cookies')\n&gt;&gt;&gt; jar.set('gross_cookie', 'blech', domain='httpbin.org', path='\/elsewhere')\n&gt;&gt;&gt; url = 'http:\/\/httpbin.org\/cookies'\n&gt;&gt;&gt; r = requests.get(url, cookies=jar)\n&gt;&gt;&gt; r.text\n'{\"cookies\": {\"tasty_cookie\": \"yum\"}}'\n<\/code><\/pre>\n<h2>\u91cd\u5b9a\u5411\u4e0e\u8bf7\u6c42\u5386\u53f2\u00b6<\/h2>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u9664\u4e86 HEAD, Requests \u4f1a\u81ea\u52a8\u5904\u7406\u6240\u6709\u91cd\u5b9a\u5411\u3002<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u54cd\u5e94\u5bf9\u8c61\u7684 <code>history<\/code> \u65b9\u6cd5\u6765\u8ffd\u8e2a\u91cd\u5b9a\u5411\u3002<\/p>\n<p><code>Response.history<\/code> \u662f\u4e00\u4e2a <code>Response<\/code>  \u5bf9\u8c61\u7684\u5217\u8868\uff0c\u4e3a\u4e86\u5b8c\u6210\u8bf7\u6c42\u800c\u521b\u5efa\u4e86\u8fd9\u4e9b\u5bf9\u8c61\u3002\u8fd9\u4e2a\u5bf9\u8c61\u5217\u8868\u6309\u7167\u4ece\u6700\u8001\u5230\u6700\u8fd1\u7684\u8bf7\u6c42\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p>\u4f8b\u5982\uff0cGithub \u5c06\u6240\u6709\u7684 HTTP \u8bf7\u6c42\u91cd\u5b9a\u5411\u5230 HTTPS\uff1a<\/p>\n<pre><code>\n&gt;&gt;&gt; r = requests.get('http:\/\/github.com')\n\n&gt;&gt;&gt; r.url\n'https:\/\/github.com\/'\n\n&gt;&gt;&gt; r.status_code\n200\n\n&gt;&gt;&gt; r.history\n[&lt;Response [301]&gt;]\n\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fGET\u3001OPTIONS\u3001POST\u3001PUT\u3001PATCH \u6216\u8005 DELETE\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>allow_redirects<\/code><br \/>\u53c2\u6570\u7981\u7528\u91cd\u5b9a\u5411\u5904\u7406\uff1a<\/p>\n<pre><code>\n&gt;&gt;&gt; r = requests.get('http:\/\/github.com', allow_redirects=False)\n&gt;&gt;&gt; r.status_code\n301\n&gt;&gt;&gt; r.history\n[]\n\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u4f7f\u7528\u4e86 HEAD\uff0c\u4f60\u4e5f\u53ef\u4ee5\u542f\u7528\u91cd\u5b9a\u5411\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; r = requests.head('http:\/\/github.com', allow_redirects=True)\n&gt;&gt;&gt; r.url\n'https:\/\/github.com\/'\n&gt;&gt;&gt; r.history\n[&lt;Response [301]&gt;]\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Requests \u552f\u4e00\u7684\u4e00\u4e2a\u975e\u8f6c\u57fa\u56e0\u7684 Python HTTP \u5e93\uff0c\u4eba\u7c7b\u53ef\u4ee5\u5b89\u5168\u4eab\u7528\u3002 \u8b66\u544a\uff1a\u975e\u4e13\u4e1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[45],"class_list":["post-245","post","type-post","status-publish","format-standard","hentry","category-learn","tag-python"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=245"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/245\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}