{"id":204903,"date":"2025-05-29T15:07:09","date_gmt":"2025-05-29T07:07:09","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204903\/"},"modified":"2025-05-29T15:07:09","modified_gmt":"2025-05-29T07:07:09","slug":"python-requests-%e5%ba%93%e7%9a%84%e9%bb%98%e8%ae%a4%e8%b6%85%e6%97%b6%e6%97%b6%e9%97%b4%e6%98%af%e6%80%8e%e6%a0%b7%e7%9a%84%ef%bc%9f%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e8%b6%85%e6%97%b6%e6%97%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204903\/","title":{"rendered":"Python Requests \u5e93\u7684\u9ed8\u8ba4\u8d85\u65f6\u65f6\u95f4\u662f\u600e\u6837\u7684\uff1f\u5982\u4f55\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4ee5\u907f\u514d\u4ee3\u7801\u65e0\u9650\u671f\u6302\u8d77\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python Requests \u5e93\u7684\u9ed8\u8ba4\u8d85\u65f6\u65f6\u95f4\u662f\u600e\u6837\u7684\uff1f\u5982\u4f55\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4ee5\u907f\u514d\u4ee3\u7801\u65e0\u9650\u671f\u6302\u8d77\uff1f<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1731980929673bee8108850.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u7406\u89e3 python requests \u5e93\u7684\u9ed8\u8ba4\u8d85\u65f6\u65f6\u95f4<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 requests \u5e93\u65f6\uff0c\u4e86\u89e3\u201c\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\u201d\u548c\u201c\u8bfb\u53d6\u8d85\u65f6\u65f6\u95f4\u201d\u7684\u9ed8\u8ba4\u503c\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u514d\u5bfc\u81f4\u4ee3\u7801\u65e0\u9650\u671f\u5730\u6302\u8d77\u3002<\/p>\n<p><strong>\u9ed8\u8ba4\u8d85\u65f6\u503c<\/strong><\/p>\n<p>\u6839\u636e\u5b98\u65b9\u6587\u6863\uff0crequests \u5e93\u7684\u9ed8\u8ba4\u8d85\u65f6\u503c\u4e3a\uff1a<\/p>\n<ul>\n<li>\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\uff1a\u65e0\uff08\u65e0\u9650\uff09<\/li>\n<li>\u8bfb\u53d6\u8d85\u65f6\u65f6\u95f4\uff1a\u65e0\uff08\u65e0\u9650\uff09<\/li>\n<\/ul>\n<p>\u8fd9\u8868\u660e\uff0c\u5728\u672a\u6307\u5b9a\u8d85\u65f6\u65f6\u95f4\u7684\u60c5\u51b5\u4e0b\uff0crequests \u8bf7\u6c42\u4e0d\u4f1a\u4e3b\u52a8\u8d85\u65f6\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u76ee\u6807\u670d\u52a1\u5668\u6ca1\u6709\u53ca\u65f6\u54cd\u5e94\uff0c\u8bf7\u6c42\u4f1a\u4e00\u76f4\u963b\u585e\u4e0b\u53bb\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u9700\u8981\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff1f<\/strong><\/p>\n<p>\u65e0\u9650\u7684\u8d85\u65f6\u65f6\u95f4\u867d\u7136\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u65b9\u4fbf\uff0c\u4f46\u4e5f\u4f1a\u5e26\u6765\u95ee\u9898\uff1a<\/p>\n<ul>\n<li><strong>\u7b49\u5f85\u65f6\u95f4\u8fc7\u957f\uff1a<\/strong>\u670d\u52a1\u5668\u54cd\u5e94\u5ef6\u8fdf\u4f1a\u5bfc\u81f4\u4f60\u7684\u7a0b\u5e8f\u957f\u65f6\u95f4\u6302\u8d77\uff0c\u751a\u81f3\u8017\u5c3d\u5185\u5b58\u3002<\/li>\n<li><strong>\u7f3a\u4e4f\u9519\u8bef\u53cd\u9988\uff1a<\/strong>\u5982\u679c\u6ca1\u6709\u8d85\u65f6\u673a\u5236\uff0c\u5373\u4f7f\u670d\u52a1\u5668\u65e0\u6cd5\u54cd\u5e94\uff0c\u4f60\u7684\u7a0b\u5e8f\u4e5f\u4e0d\u80fd\u8bc6\u522b\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p><strong>\u5982\u4f55\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4<\/strong><\/p>\n<p>\u8981\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff0c\u53ea\u9700\u5728 send() \u6216 request() \u65b9\u6cd5\u4e2d\u6307\u5b9a timeout \u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>import requests\n\n# \u8bbe\u7f6e\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\u4e3a 5 \u79d2\nresponse = requests.get(\"https:\/\/example.com\", timeout=(5, None))\n\n# \u8bbe\u7f6e\u8bfb\u53d6\u8d85\u65f6\u65f6\u95f4\u4e3a 10 \u79d2\nresponse = requests.get(\"https:\/\/example.com\", timeout=(None, 10))<\/pre>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p>\u4e3a\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u7b49\u5f85\uff0c \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u5c06\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\u8bbe\u7f6e\u4e3a\u7a0d\u9ad8\u4e8e tcp \u6570\u636e\u5305\u91cd\u4f20\u7a97\u53e3\uff08\u901a\u5e38\u4e3a 3 \u79d2\uff09\uff0c\u5e76\u5c06\u8bfb\u53d6\u8d85\u65f6\u65f6\u95f4\u8bbe\u7f6e\u4e3a\u670d\u52a1\u5668\u901a\u5e38\u54cd\u5e94\u7684\u9884\u671f\u65f6\u95f4\u3002\u8fd9\u5c06\u786e\u4fdd\u4f60\u7684\u7a0b\u5e8f\u5728\u5408\u7406\u7684\u65f6\u95f4\u5185\u6536\u5230\u54cd\u5e94\uff0c\u6216\u53ca\u65f6\u8bc6\u522b\u9519\u8bef\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPython Requests \u5e93\u7684\u9ed8\u8ba4\u8d85\u65f6\u65f6\u95f4\u662f\u600e\u6837\u7684\uff1f\u5982\u4f55\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4ee5\u907f\u514d\u4ee3\u7801\u65e0\u9650\u671f\u6302\u8d77\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Requests &#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-204903","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204903","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=204903"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204903\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}