{"id":1080915,"date":"2025-01-08T12:34:42","date_gmt":"2025-01-08T04:34:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1080915.html"},"modified":"2025-01-08T12:34:45","modified_gmt":"2025-01-08T04:34:45","slug":"python%e5%a6%82%e4%bd%95%e5%90%91%e6%9c%8d%e5%8a%a1%e5%99%a8%e6%8f%90%e4%ba%a4%e6%95%b0%e6%8d%ae-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1080915.html","title":{"rendered":"python\u5982\u4f55\u5411\u670d\u52a1\u5668\u63d0\u4ea4\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183210\/307361ec-22b9-49c7-b5f6-8795ca2b88d1.webp\" alt=\"python\u5982\u4f55\u5411\u670d\u52a1\u5668\u63d0\u4ea4\u6570\u636e\" \/><\/p>\n<p><p> Python \u5411\u670d\u52a1\u5668\u63d0\u4ea4\u6570\u636e\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a<strong>\u4f7f\u7528HTTP\u8bf7\u6c42\u3001\u4f7f\u7528requests\u5e93\u3001\u4f7f\u7528urllib\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u4e14\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528requests\u5e93\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5411\u670d\u52a1\u5668\u63d0\u4ea4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528HTTP\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>HTTP\u534f\u8bae\u662f\u7f51\u7edc\u901a\u4fe1\u7684\u57fa\u7840\u534f\u8bae\uff0c\u5b83\u5b9a\u4e49\u4e86\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u8f93\u683c\u5f0f\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u7b80\u5316HTTP\u8bf7\u6c42\u7684\u53d1\u9001\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528requests\u5e93<\/h4>\n<\/p>\n<p><p>requests\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684HTTP\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b80\u6d01\u548c\u4f18\u96c5\u7684API\u6765\u5904\u7406HTTP\u8bf7\u6c42\u3002\u5b83\u652f\u6301\u5404\u79cdHTTP\u65b9\u6cd5\uff08\u5982GET\u3001POST\u3001PUT\u3001DELETE\u7b49\uff09\uff0c\u5e76\u80fd\u65b9\u4fbf\u5730\u5904\u7406Cookies\u3001Session\u3001\u6587\u4ef6\u4e0a\u4f20\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;http:\/\/example.com\/api&#39;<\/p>\n<p>data = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p>response = requests.post(url, data=data)<\/p>\n<p>print(response.status_code)<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528POST\u65b9\u6cd5\u5411\u670d\u52a1\u5668\u63d0\u4ea4\u6570\u636e\u3002<strong>requests.post()<\/strong>\u51fd\u6570\u53d1\u9001\u4e00\u4e2aPOST\u8bf7\u6c42\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a<strong>Response<\/strong>\u5bf9\u8c61\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<strong>response.status_code<\/strong>\u67e5\u770bHTTP\u72b6\u6001\u7801\uff0c\u901a\u8fc7<strong>response.text<\/strong>\u67e5\u770b\u670d\u52a1\u5668\u8fd4\u56de\u7684\u54cd\u5e94\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528GET\u65b9\u6cd5\u63d0\u4ea4\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u867d\u7136GET\u65b9\u6cd5\u4e3b\u8981\u7528\u4e8e\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e\uff0c\u4f46\u4e5f\u53ef\u4ee5\u901a\u8fc7\u67e5\u8be2\u5b57\u7b26\u4e32\u63d0\u4ea4\u6570\u636e\u3002\u67e5\u8be2\u5b57\u7b26\u4e32\u662fURL\u7684\u4e00\u90e8\u5206\uff0c\u901a\u5e38\u4ee5\u95ee\u53f7\uff08?\uff09\u5f00\u59cb\uff0c\u540e\u9762\u8ddf\u7740\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">url = &#39;http:\/\/example.com\/api&#39;<\/p>\n<p>params = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p>response = requests.get(url, params=params)<\/p>\n<p>print(response.status_code)<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528GET\u65b9\u6cd5\u5411\u670d\u52a1\u5668\u63d0\u4ea4\u6570\u636e\u3002<strong>requests.get()<\/strong>\u51fd\u6570\u4f1a\u5c06\u53c2\u6570\u81ea\u52a8\u7f16\u7801\u4e3a\u67e5\u8be2\u5b57\u7b26\u4e32\uff0c\u5e76\u9644\u52a0\u5230URL\u540e\u9762\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528urllib\u5e93<\/h3>\n<\/p>\n<p><p>urllib\u662fPython\u5185\u7f6e\u7684\u5e93\uff0c\u5305\u542b\u4e86\u4e00\u7ec4\u7528\u4e8e\u64cd\u4f5cURL\u7684\u6a21\u5757\u3002\u867d\u7136urllib\u7684API\u4e0d\u5982requests\u5e93\u7b80\u6d01\uff0c\u4f46\u5b83\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528urllib.request\u6a21\u5757<\/h4>\n<\/p>\n<p><p>urllib.request\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406HTTP\u8bf7\u6c42\u7684\u57fa\u672c\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<strong>urllib.request.urlopen()<\/strong>\u51fd\u6570\u53d1\u9001HTTP\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import urllib.parse<\/p>\n<p>import urllib.request<\/p>\n<p>url = &#39;http:\/\/example.com\/api&#39;<\/p>\n<p>data = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p>data = urllib.parse.urlencode(data).encode()<\/p>\n<p>req = urllib.request.Request(url, data=data)<\/p>\n<p>response = urllib.request.urlopen(req)<\/p>\n<p>print(response.status)<\/p>\n<p>print(response.read().decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528POST\u65b9\u6cd5\u5411\u670d\u52a1\u5668\u63d0\u4ea4\u6570\u636e\u3002\u9996\u5148\uff0c\u6211\u4eec\u4f7f\u7528<strong>urllib.parse.urlencode()<\/strong>\u51fd\u6570\u5c06\u6570\u636e\u7f16\u7801\u4e3a\u67e5\u8be2\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\u3002\u63a5\u7740\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a<strong>Request<\/strong>\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<strong>urllib.request.urlopen()<\/strong>\u51fd\u6570\u53d1\u9001\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528GET\u65b9\u6cd5\u63d0\u4ea4\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528GET\u65b9\u6cd5\u901a\u8fc7\u67e5\u8be2\u5b57\u7b26\u4e32\u63d0\u4ea4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import urllib.parse<\/p>\n<p>import urllib.request<\/p>\n<p>url = &#39;http:\/\/example.com\/api&#39;<\/p>\n<p>params = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p>url += &#39;?&#39; + urllib.parse.urlencode(params)<\/p>\n<p>response = urllib.request.urlopen(url)<\/p>\n<p>print(response.status)<\/p>\n<p>print(response.read().decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528GET\u65b9\u6cd5\u5411\u670d\u52a1\u5668\u63d0\u4ea4\u6570\u636e\u3002\u6211\u4eec\u624b\u52a8\u5c06\u53c2\u6570\u7f16\u7801\u4e3a\u67e5\u8be2\u5b57\u7b26\u4e32\uff0c\u5e76\u9644\u52a0\u5230URL\u540e\u9762\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528JSON\u683c\u5f0f\u63d0\u4ea4\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u73b0\u4ee3Web\u5e94\u7528\u4e2d\uff0cJSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\u3002Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684<strong>json<\/strong>\u6a21\u5757\u6765\u5904\u7406JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528requests\u5e93\u63d0\u4ea4JSON\u6570\u636e<\/h4>\n<\/p>\n<p><p>requests\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5904\u7406JSON\u6570\u636e\u3002\u6211\u4eec\u53ea\u9700\u5c06\u6570\u636e\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u5e76\u8bbe\u7f6e\u8bf7\u6c42\u5934\u4e2d\u7684<strong>Content-Type<\/strong>\u4e3a<strong>application\/json<\/strong>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>import json<\/p>\n<p>url = &#39;http:\/\/example.com\/api&#39;<\/p>\n<p>data = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p>headers = {&#39;Content-Type&#39;: &#39;application\/json&#39;}<\/p>\n<p>response = requests.post(url, data=json.dumps(data), headers=headers)<\/p>\n<p>print(response.status_code)<\/p>\n<p>print(response.json())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528POST\u65b9\u6cd5\u63d0\u4ea4JSON\u6570\u636e\u3002<strong>json.dumps()<\/strong>\u51fd\u6570\u5c06\u6570\u636e\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u5e76\u901a\u8fc7<strong>headers<\/strong>\u53c2\u6570\u8bbe\u7f6e\u8bf7\u6c42\u5934\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528urllib\u5e93\u63d0\u4ea4JSON\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528urllib\u5e93\u63d0\u4ea4JSON\u6570\u636e\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002\u6211\u4eec\u9700\u8981\u624b\u52a8\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff0c\u5e76\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import urllib.parse<\/p>\n<p>import urllib.request<\/p>\n<p>import json<\/p>\n<p>url = &#39;http:\/\/example.com\/api&#39;<\/p>\n<p>data = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p>data = json.dumps(data).encode()<\/p>\n<p>req = urllib.request.Request(url, data=data)<\/p>\n<p>req.add_header(&#39;Content-Type&#39;, &#39;application\/json&#39;)<\/p>\n<p>response = urllib.request.urlopen(req)<\/p>\n<p>print(response.status)<\/p>\n<p>print(response.read().decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528POST\u65b9\u6cd5\u63d0\u4ea4JSON\u6570\u636e\u3002\u9996\u5148\uff0c\u6211\u4eec\u4f7f\u7528<strong>json.dumps()<\/strong>\u51fd\u6570\u5c06\u6570\u636e\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\u3002\u63a5\u7740\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a<strong>Request<\/strong>\u5bf9\u8c61\uff0c\u5e76\u624b\u52a8\u8bbe\u7f6e\u8bf7\u6c42\u5934\u4e2d\u7684<strong>Content-Type<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u670d\u52a1\u5668\u54cd\u5e94<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u63d0\u4ea4\u6570\u636e\uff0c\u5904\u7406\u670d\u52a1\u5668\u54cd\u5e94\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u6b65\u9aa4\u3002\u901a\u5e38\uff0c\u670d\u52a1\u5668\u4f1a\u8fd4\u56de\u4e00\u4e2a\u54cd\u5e94\uff0c\u5305\u542b\u72b6\u6001\u7801\u3001\u54cd\u5e94\u5934\u548c\u54cd\u5e94\u4f53\u3002<\/p>\n<\/p>\n<p><h4>1. \u5904\u7406\u72b6\u6001\u7801<\/h4>\n<\/p>\n<p><p>\u72b6\u6001\u7801\u662fHTTP\u54cd\u5e94\u7684\u4e00\u90e8\u5206\uff0c\u8868\u793a\u8bf7\u6c42\u7684\u7ed3\u679c\u3002\u5e38\u89c1\u7684\u72b6\u6001\u7801\u5305\u62ec200\uff08\u6210\u529f\uff09\u3001404\uff08\u672a\u627e\u5230\uff09\u3001500\uff08\u670d\u52a1\u5668\u9519\u8bef\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;http:\/\/example.com\/api&#39;)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    print(&#39;Request was successful&#39;)<\/p>\n<p>elif response.status_code == 404:<\/p>\n<p>    print(&#39;Resource not found&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;An error occurred:&#39;, response.status_code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u68c0\u67e5\u54cd\u5e94\u7684\u72b6\u6001\u7801\uff0c\u5e76\u6839\u636e\u4e0d\u540c\u7684\u72b6\u6001\u7801\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>2. \u5904\u7406\u54cd\u5e94\u5934<\/h4>\n<\/p>\n<p><p>\u54cd\u5e94\u5934\u662fHTTP\u54cd\u5e94\u7684\u4e00\u90e8\u5206\uff0c\u5305\u542b\u5173\u4e8e\u54cd\u5e94\u7684\u5143\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<strong>headers<\/strong>\u5c5e\u6027\u8bbf\u95ee\u54cd\u5e94\u5934\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;http:\/\/example.com\/api&#39;)<\/p>\n<p>print(response.headers)<\/p>\n<p>content_type = response.headers.get(&#39;Content-Type&#39;)<\/p>\n<p>print(&#39;Content-Type:&#39;, content_type)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6253\u5370\u4e86\u54cd\u5e94\u5934\uff0c\u5e76\u63d0\u53d6\u4e86<strong>Content-Type<\/strong>\u5b57\u6bb5\u3002<\/p>\n<\/p>\n<p><h4>3. \u5904\u7406\u54cd\u5e94\u4f53<\/h4>\n<\/p>\n<p><p>\u54cd\u5e94\u4f53\u662fHTTP\u54cd\u5e94\u7684\u4e00\u90e8\u5206\uff0c\u5305\u542b\u670d\u52a1\u5668\u8fd4\u56de\u7684\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<strong>text<\/strong>\u5c5e\u6027\uff08\u5b57\u7b26\u4e32\uff09\u6216<strong>content<\/strong>\u5c5e\u6027\uff08\u5b57\u8282\uff09\u8bbf\u95ee\u54cd\u5e94\u4f53\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;http:\/\/example.com\/api&#39;)<\/p>\n<p>print(response.text)<\/p>\n<p>data = response.json()<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6253\u5370\u4e86\u54cd\u5e94\u4f53\uff0c\u5e76\u5c06\u5176\u89e3\u6790\u4e3aJSON\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u901a\u4fe1\u4e2d\uff0c\u9519\u8bef\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u6211\u4eec\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u5904\u7406\u5404\u79cd\u53ef\u80fd\u7684\u9519\u8bef\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528requests\u5e93\u5904\u7406\u9519\u8bef<\/h4>\n<\/p>\n<p><p>requests\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<strong>try-except<\/strong>\u8bed\u53e5\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u5904\u7406\u5404\u79cd\u53ef\u80fd\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>try:<\/p>\n<p>    response = requests.get(&#39;http:\/\/example.com\/api&#39;)<\/p>\n<p>    response.r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se_for_status()<\/p>\n<p>except requests.exceptions.HTTPError as errh:<\/p>\n<p>    print(&#39;HTTP Error:&#39;, errh)<\/p>\n<p>except requests.exceptions.ConnectionError as errc:<\/p>\n<p>    print(&#39;Error Connecting:&#39;, errc)<\/p>\n<p>except requests.exceptions.Timeout as errt:<\/p>\n<p>    print(&#39;Timeout Error:&#39;, errt)<\/p>\n<p>except requests.exceptions.RequestException as err:<\/p>\n<p>    print(&#39;Error:&#39;, err)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;Request was successful&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6355\u83b7\u4e86\u5404\u79cd\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u5e76\u6839\u636e\u5f02\u5e38\u7c7b\u578b\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528urllib\u5e93\u5904\u7406\u9519\u8bef<\/h4>\n<\/p>\n<p><p>urllib\u5e93\u4e5f\u63d0\u4f9b\u4e86\u9519\u8bef\u5904\u7406\u673a\u5236\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<strong>try-except<\/strong>\u8bed\u53e5\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u5904\u7406\u5404\u79cd\u53ef\u80fd\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import urllib.request<\/p>\n<p>try:<\/p>\n<p>    response = urllib.request.urlopen(&#39;http:\/\/example.com\/api&#39;)<\/p>\n<p>except urllib.error.HTTPError as errh:<\/p>\n<p>    print(&#39;HTTP Error:&#39;, errh)<\/p>\n<p>except urllib.error.URLError as erru:<\/p>\n<p>    print(&#39;URL Error:&#39;, erru)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;Request was successful&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6355\u83b7\u4e86\u5404\u79cd\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u5e76\u6839\u636e\u5f02\u5e38\u7c7b\u578b\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u5411\u670d\u52a1\u5668\u63d0\u4ea4\u6570\u636e\u7684\u5404\u79cd\u65b9\u6cd5\u3002\u6211\u4eec\u4e3b\u8981\u8ba8\u8bba\u4e86\u4f7f\u7528requests\u5e93\u548curllib\u5e93\u5904\u7406HTTP\u8bf7\u6c42\uff0c\u5e76\u4ecb\u7ecd\u4e86\u5982\u4f55\u5904\u7406JSON\u6570\u636e\u3001\u54cd\u5e94\u548c\u9519\u8bef\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u8fdb\u884c\u7f51\u7edc\u901a\u4fe1\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u8bf7\u968f\u65f6\u4e0e\u6211\u8054\u7cfb\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u63d0\u4ea4\u6570\u636e\u5230\u670d\u52a1\u5668\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u63d0\u4ea4\u6570\u636e\u5230\u670d\u52a1\u5668\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7HTTP\u8bf7\u6c42\u6765\u5b9e\u73b0\u3002\u6700\u5e38\u7528\u7684\u5e93\u662f<code>requests<\/code>\uff0c\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684API\u6765\u8fdb\u884cGET\u548cPOST\u8bf7\u6c42\u3002\u4f7f\u7528<code>requests.post()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u636e\u4f5c\u4e3a\u8868\u5355\u6216JSON\u683c\u5f0f\u53d1\u9001\u5230\u6307\u5b9a\u7684URL\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import requests\n\nurl = &#39;http:\/\/example.com\/api&#39;\ndata = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}\nresponse = requests.post(url, json=data)\nprint(response.status_code)\nprint(response.json())\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5411\u670d\u52a1\u5668\u63d0\u4ea4\u4e86\u4e00\u4e2aJSON\u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<p><strong>Python\u652f\u6301\u54ea\u4e9b\u6570\u636e\u683c\u5f0f\u8fdb\u884c\u63d0\u4ea4\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u901a\u8fc7<code>requests<\/code>\u5e93\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u63d0\u4ea4\uff0c\u5305\u62ec\u8868\u5355\u6570\u636e\u3001JSON\u6570\u636e\u3001\u6587\u4ef6\u4e0a\u4f20\u4ee5\u53caXML\u7b49\u3002\u901a\u8fc7<code>data<\/code>\u53c2\u6570\u53ef\u4ee5\u53d1\u9001\u8868\u5355\u6570\u636e\uff0c\u901a\u8fc7<code>json<\/code>\u53c2\u6570\u53ef\u4ee5\u53d1\u9001JSON\u683c\u5f0f\u7684\u6570\u636e\uff0c\u6587\u4ef6\u4e0a\u4f20\u5219\u53ef\u4ee5\u4f7f\u7528<code>files<\/code>\u53c2\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">files = {&#39;file&#39;: open(&#39;report.xlsx&#39;, &#39;rb&#39;)}\nresponse = requests.post(url, files=files)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97Python\u80fd\u591f\u9002\u5e94\u4e0d\u540c\u7684API\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u670d\u52a1\u5668\u8fd4\u56de\u7684\u54cd\u5e94\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\u4f7f\u7528<code>requests<\/code>\u5e93\u63d0\u4ea4\u6570\u636e\u540e\uff0c\u670d\u52a1\u5668\u901a\u5e38\u4f1a\u8fd4\u56de\u54cd\u5e94\u3002\u53ef\u4ee5\u901a\u8fc7<code>response<\/code>\u5bf9\u8c61\u6765\u83b7\u53d6\u72b6\u6001\u7801\u3001\u54cd\u5e94\u5185\u5bb9\u4ee5\u53ca\u54cd\u5e94\u5934\u7b49\u4fe1\u606f\u3002\u4f7f\u7528<code>response.json()<\/code>\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6JSON\u683c\u5f0f\u7684\u54cd\u5e94\u6570\u636e\uff0c\u4f7f\u7528<code>response.text<\/code>\u53ef\u4ee5\u83b7\u53d6\u539f\u59cb\u6587\u672c\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">if response.status_code == 200:\n    response_data = response.json()\n    print(&quot;\u54cd\u5e94\u6570\u636e:&quot;, response_data)\nelse:\n    print(&quot;\u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801:&quot;, response.status_code)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u9ad8\u6548\u5730\u5904\u7406\u670d\u52a1\u5668\u7684\u53cd\u9988\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u6570\u636e\u5904\u7406\u548c\u903b\u8f91\u63a7\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5411\u670d\u52a1\u5668\u63d0\u4ea4\u6570\u636e\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528HTTP\u8bf7\u6c42\u3001\u4f7f\u7528requests\u5e93\u3001\u4f7f\u7528urllib\u5e93\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1080925,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080915"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1080915"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080915\/revisions"}],"predecessor-version":[{"id":1080928,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080915\/revisions\/1080928"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1080925"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1080915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1080915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1080915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}