{"id":995399,"date":"2024-12-27T09:04:32","date_gmt":"2024-12-27T01:04:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/995399.html"},"modified":"2024-12-27T09:04:34","modified_gmt":"2024-12-27T01:04:34","slug":"python%e5%a6%82%e4%bd%95api%e8%b0%83%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/995399.html","title":{"rendered":"python\u5982\u4f55api\u8c03\u7528\u65b9\u6cd5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072013\/9f6283b8-2b10-4e45-b4c6-2d00e9af6d2c.webp\" alt=\"python\u5982\u4f55api\u8c03\u7528\u65b9\u6cd5\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u8fdb\u884cAPI\u8c03\u7528\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u5e93\u5982<code>urllib<\/code>\u3001\u7b2c\u4e09\u65b9\u5e93\u5982<code>requests<\/code>\uff0c\u4ee5\u53ca\u5229\u7528\u5f02\u6b65\u5e93\u5982<code><a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ohttp<\/code>\u7b49\u3002<\/strong>\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528<code>requests<\/code>\u5e93\u662f\u6700\u4e3a\u7b80\u5355\u548c\u6d41\u884c\u7684\u4e00\u79cd\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684API\u548c\u4e30\u5bcc\u7684\u529f\u80fd\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>requests<\/code>\u5e93\u8fdb\u884cAPI\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>requests<\/code>\u5e93\u8fdb\u884cAPI\u8c03\u7528\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5\u5e93\u3001\u6784\u5efa\u8bf7\u6c42\u3001\u5904\u7406\u54cd\u5e94\u3001\u5904\u7406\u9519\u8bef\u4ee5\u53ca\u6570\u636e\u89e3\u6790\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u5e76\u4ecb\u7ecd\u5982\u4f55\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u4e0e\u57fa\u672c\u4f7f\u7528<\/p>\n<\/p>\n<p><p>Python\u7684<code>requests<\/code>\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684HTTP\u5e93\uff0c\u5b83\u53ef\u4ee5\u8f7b\u677e\u5730\u53d1\u9001HTTP\u8bf7\u6c42\u3002\u8981\u4f7f\u7528<code>requests<\/code>\u5e93\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5b83\u5df2\u7ecf\u5b89\u88c5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5requests\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u5b89\u88c5<code>requests<\/code>\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4f1a\u5728\u4f60\u7684Python\u73af\u5883\u4e2d\u5b89\u88c5<code>requests<\/code>\u5e93\uff0c\u786e\u4fdd\u4f60\u80fd\u591f\u4f7f\u7528\u5b83\u6765\u8fdb\u884cAPI\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u53d1\u9001GET\u8bf7\u6c42<\/strong><\/p>\n<\/p>\n<p><p>\u53d1\u9001GET\u8bf7\u6c42\u662fAPI\u8c03\u7528\u4e2d\u6700\u5e38\u7528\u7684\u64cd\u4f5c\u4e4b\u4e00\u3002GET\u8bf7\u6c42\u7528\u4e8e\u4ece\u670d\u52a1\u5668\u8bf7\u6c42\u6570\u636e\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u67d0\u4e2a\u57ce\u5e02\u7684\u5929\u6c14\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;https:\/\/api.openweathermap.org\/data\/2.5\/weather&#39;, params={<\/p>\n<p>    &#39;q&#39;: &#39;London&#39;,<\/p>\n<p>    &#39;appid&#39;: &#39;your_api_key&#39;<\/p>\n<p>})<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    data = response.json()<\/p>\n<p>    print(data)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Failed to retrieve data:&quot;, response.status_code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>requests.get()<\/code>\u51fd\u6570\u53d1\u9001\u4e00\u4e2aGET\u8bf7\u6c42\uff0c\u5e76\u901a\u8fc7<code>params<\/code>\u53c2\u6570\u4f20\u9012\u67e5\u8be2\u53c2\u6570\u3002\u54cd\u5e94\u6570\u636e\u901a\u8fc7<code>response.json()<\/code>\u65b9\u6cd5\u8f6c\u6362\u4e3aPython\u5b57\u5178\u683c\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u6784\u5efa\u590d\u6742\u8bf7\u6c42<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884cAPI\u8c03\u7528\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u6784\u5efa\u66f4\u590d\u6742\u7684\u8bf7\u6c42\uff0c\u5305\u62ec\u53d1\u9001POST\u8bf7\u6c42\u3001\u6dfb\u52a0\u8bf7\u6c42\u5934\u3001\u5904\u7406\u8d85\u65f6\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u53d1\u9001POST\u8bf7\u6c42<\/strong><\/p>\n<\/p>\n<p><p>POST\u8bf7\u6c42\u7528\u4e8e\u5411\u670d\u52a1\u5668\u53d1\u9001\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5411\u4e00\u4e2aAPI\u7aef\u70b9\u53d1\u9001\u7528\u6237\u6ce8\u518c\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>data = {<\/p>\n<p>    &#39;username&#39;: &#39;new_user&#39;,<\/p>\n<p>    &#39;password&#39;: &#39;secure_password&#39;<\/p>\n<p>}<\/p>\n<p>response = requests.post(&#39;https:\/\/example.com\/api\/register&#39;, json=data)<\/p>\n<p>if response.status_code == 201:<\/p>\n<p>    print(&quot;User registered successfully!&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Registration failed:&quot;, response.status_code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>requests.post()<\/code>\u51fd\u6570\u53d1\u9001\u4e00\u4e2aPOST\u8bf7\u6c42\uff0c\u5e76\u901a\u8fc7<code>json<\/code>\u53c2\u6570\u4f20\u9012\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u8bf7\u6c42\u5934<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u5728\u8bf7\u6c42\u4e2d\u6dfb\u52a0\u7279\u5b9a\u7684\u5934\u4fe1\u606f\uff0c\u4f8b\u5982\u6388\u6743\u4ee4\u724c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">headers = {<\/p>\n<p>    &#39;Authorization&#39;: &#39;Bearer your_access_token&#39;<\/p>\n<p>}<\/p>\n<p>response = requests.get(&#39;https:\/\/api.example.com\/data&#39;, headers=headers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5728\u8bf7\u6c42\u4e2d\u6dfb\u52a0<code>headers<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u8bbe\u7f6eHTTP\u5934\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u8d85\u65f6<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u67d0\u4e9bAPI\u8c03\u7528\uff0c\u53ef\u80fd\u9700\u8981\u8bbe\u7f6e\u8bf7\u6c42\u7684\u8d85\u65f6\u65f6\u95f4\uff0c\u4ee5\u907f\u514d\u957f\u65f6\u95f4\u7b49\u5f85\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    response = requests.get(&#39;https:\/\/api.example.com\/data&#39;, timeout=5)<\/p>\n<p>except requests.Timeout:<\/p>\n<p>    print(&quot;The request timed out&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u8bf7\u6c42\u7684\u8d85\u65f6\u65f6\u95f4\u4e3a5\u79d2\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5904\u7406\u54cd\u5e94\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5904\u7406\u54cd\u5e94\u6570\u636e\u662fAPI\u8c03\u7528\u7684\u91cd\u8981\u90e8\u5206\uff0c\u5305\u62ec\u89e3\u6790JSON\u6570\u636e\u3001\u5904\u7406\u9519\u8bef\u54cd\u5e94\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u89e3\u6790JSON\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u5927\u591a\u6570API\u8fd4\u56de\u7684\u6570\u636e\u90fd\u662fJSON\u683c\u5f0f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528<code>response.json()<\/code>\u65b9\u6cd5\u8fdb\u884c\u89e3\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = response.json()<\/p>\n<p>print(data[&#39;key&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u89e3\u6790JSON\u6570\u636e\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95eeAPI\u8fd4\u56de\u7684\u6570\u636e\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u9519\u8bef\u54cd\u5e94<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884cAPI\u8c03\u7528\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u9519\u8bef\u54cd\u5e94\u3002\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u54cd\u5e94\u72b6\u6001\u7801\u6765\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if response.status_code != 200:<\/p>\n<p>    print(&quot;Error:&quot;, response.status_code)<\/p>\n<p>    print(&quot;Details:&quot;, response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u68c0\u67e5\u72b6\u6001\u7801\uff0c\u53ef\u4ee5\u5224\u65ad\u8bf7\u6c42\u662f\u5426\u6210\u529f\uff0c\u5e76\u83b7\u53d6\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5f02\u6b65API\u8c03\u7528<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u8bf7\u6c42\u6216\u9700\u8981\u63d0\u9ad8\u54cd\u5e94\u901f\u5ea6\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u6b65\u5e93\u5982<code>aiohttp<\/code>\u8fdb\u884cAPI\u8c03\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5aiohttp\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528<code>aiohttp<\/code>\u5e93\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install aiohttp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5f02\u6b65GET\u8bf7\u6c42<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>aiohttp<\/code>\u5e93\u8fdb\u884c\u5f02\u6b65\u8bf7\u6c42\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u8bf7\u6c42\u7684\u5e76\u53d1\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import aiohttp<\/p>\n<p>import asyncio<\/p>\n<p>async def fetch_data(url):<\/p>\n<p>    async with aiohttp.ClientSession() as session:<\/p>\n<p>        async with session.get(url) as response:<\/p>\n<p>            return await response.json()<\/p>\n<p>async def main():<\/p>\n<p>    url = &#39;https:\/\/api.example.com\/data&#39;<\/p>\n<p>    data = await fetch_data(url)<\/p>\n<p>    print(data)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>aiohttp.ClientSession()<\/code>\u521b\u5efa\u4e00\u4e2a\u4f1a\u8bdd\uff0c\u5e76\u901a\u8fc7<code>session.get()<\/code>\u65b9\u6cd5\u53d1\u9001GET\u8bf7\u6c42\u3002\u8bf7\u6c42\u548c\u54cd\u5e94\u90fd\u5728\u5f02\u6b65\u73af\u5883\u4e2d\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5e94\u7528\u573a\u666f\u4e0e\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cAPI\u8c03\u7528\u6d89\u53ca\u591a\u4e2a\u573a\u666f\uff0c\u5305\u62ec\u6570\u636e\u6293\u53d6\u3001\u6570\u636e\u540c\u6b65\u3001\u7b2c\u4e09\u65b9\u670d\u52a1\u96c6\u6210\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u548c\u4f18\u5316\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6279\u91cf\u6570\u636e\u6293\u53d6<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6279\u91cf\u6570\u636e\u6293\u53d6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u6b65\u8bf7\u6c42\u63d0\u9ad8\u6548\u7387\u3002\u540c\u65f6\uff0c\u4f7f\u7528\u5206\u9875\u6280\u672f\u51cf\u5c11\u5355\u6b21\u8bf7\u6c42\u7684\u6570\u636e\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def fetch_all_data(base_url, pages):<\/p>\n<p>    tasks = []<\/p>\n<p>    for page in range(1, pages + 1):<\/p>\n<p>        url = f&quot;{base_url}?page={page}&quot;<\/p>\n<p>        tasks.append(fetch_data(url))<\/p>\n<p>    results = await asyncio.gather(*tasks)<\/p>\n<p>    return results<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u91cd\u8bd5\u673a\u5236<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u4e0d\u7a33\u5b9a\u6216API\u670d\u52a1\u4e0d\u53ef\u9760\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u91cd\u8bd5\u673a\u5236\u5904\u7406\u4e34\u65f6\u6545\u969c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def fetch_with_retry(url, retries=3, delay=2):<\/p>\n<p>    for attempt in range(retries):<\/p>\n<p>        try:<\/p>\n<p>            response = requests.get(url)<\/p>\n<p>            if response.status_code == 200:<\/p>\n<p>                return response.json()<\/p>\n<p>        except requests.RequestException as e:<\/p>\n<p>            print(f&quot;Attempt {attempt + 1} failed: {e}&quot;)<\/p>\n<p>            time.sleep(delay)<\/p>\n<p>    return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u91cd\u8bd5\u6b21\u6570\u548c\u5ef6\u8fdf\u65f6\u95f4\uff0c\u53ef\u4ee5\u5728\u8bf7\u6c42\u5931\u8d25\u65f6\u81ea\u52a8\u91cd\u8bd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f13\u5b58\u673a\u5236<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u51cf\u5c11\u5bf9API\u670d\u52a1\u5668\u7684\u538b\u529b\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u673a\u5236\u5b58\u50a8\u9891\u7e41\u8bf7\u6c42\u7684\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests_cache<\/p>\n<p>requests_cache.install_cache(&#39;api_cache&#39;, expire_after=300)<\/p>\n<p>response = requests.get(&#39;https:\/\/api.example.com\/data&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>requests_cache<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8bf7\u6c42\u7f13\u5b58\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6280\u672f\u548c\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u5730\u8fdb\u884cAPI\u8c03\u7528\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u4f18\u5316\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8API\u8c03\u7528\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884cAPI\u8c03\u7528\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8fdb\u884cAPI\u8c03\u7528\u901a\u5e38\u6d89\u53ca\u4f7f\u7528<code>requests<\/code>\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86\u8be5\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install requests<\/code>\u8fdb\u884c\u5b89\u88c5\u3002API\u8c03\u7528\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\u6784\u5efa\u8bf7\u6c42URL\u3001\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff08\u5982\u679c\u9700\u8981\uff09\u3001\u53d1\u9001\u8bf7\u6c42\u5e76\u5904\u7406\u54cd\u5e94\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import requests\n\nurl = &#39;https:\/\/api.example.com\/data&#39;\nresponse = requests.get(url)\n\nif response.status_code == 200:\n    data = response.json()\n    print(data)\nelse:\n    print(f&quot;Error: {response.status_code}&quot;)\n<\/code><\/pre>\n<p><strong>API\u8c03\u7528\u65f6\u5982\u4f55\u5904\u7406\u8ba4\u8bc1\u95ee\u9898\uff1f<\/strong><br \/>\u5f88\u591aAPI\u9700\u8981\u8ba4\u8bc1\u624d\u80fd\u4f7f\u7528\uff0c\u5e38\u89c1\u7684\u65b9\u5f0f\u5305\u62ecAPI\u5bc6\u94a5\u3001OAuth\u7b49\u3002\u5728\u4f7f\u7528Python\u8fdb\u884cAPI\u8c03\u7528\u65f6\uff0c\u53ef\u4ee5\u5728\u8bf7\u6c42\u5934\u4e2d\u6dfb\u52a0\u8ba4\u8bc1\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528API\u5bc6\u94a5\u65f6\uff0c\u53ef\u4ee5\u8fd9\u6837\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff1a<\/p>\n<pre><code class=\"language-python\">headers = {\n    &#39;Authorization&#39;: &#39;Bearer YOUR_API_KEY&#39;\n}\nresponse = requests.get(url, headers=headers)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u8c03\u8bd5Python API\u8c03\u7528\u4e2d\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u8c03\u8bd5API\u8c03\u7528\u4e2d\u7684\u9519\u8bef\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u54cd\u5e94\u72b6\u6001\u7801\u548c\u54cd\u5e94\u5185\u5bb9\u6765\u8fdb\u884c\u3002\u4f7f\u7528<code>response.status_code<\/code>\u68c0\u67e5\u72b6\u6001\u7801\uff0c\u5e38\u89c1\u7684\u72b6\u6001\u7801\u5305\u62ec200\uff08\u6210\u529f\uff09\u3001400\uff08\u9519\u8bef\u8bf7\u6c42\uff09\u3001401\uff08\u672a\u6388\u6743\uff09\u7b49\u3002\u5bf9\u4e8e\u66f4\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u6253\u5370<code>response.text<\/code>\u6216<code>response.json()<\/code>\uff0c\u8fd9\u5c06\u63d0\u4f9b\u66f4\u591a\u5173\u4e8e\u9519\u8bef\u7684\u4fe1\u606f\u3002\u4f7f\u7528try-except\u8bed\u53e5\u6355\u83b7\u5f02\u5e38\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u5b9a\u4f4d\u95ee\u9898\u3002<\/p>\n<pre><code class=\"language-python\">try:\n    response = requests.get(url)\n    response.raise_for_status()  # \u68c0\u67e5\u662f\u5426\u53d1\u751fHTTP\u9519\u8bef\nexcept requests.exceptions.HTTPError as err:\n    print(f&quot;HTTP error occurred: {err}&quot;)\nexcept Exception as err:\n    print(f&quot;Other error occurred: {err}&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8fdb\u884cAPI\u8c03\u7528\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u5e93\u5982urllib\u3001\u7b2c\u4e09\u65b9\u5e93\u5982requests\uff0c\u4ee5\u53ca\u5229\u7528\u5f02 [&hellip;]","protected":false},"author":3,"featured_media":995406,"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\/995399"}],"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=995399"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/995399\/revisions"}],"predecessor-version":[{"id":995409,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/995399\/revisions\/995409"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/995406"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=995399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=995399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=995399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}