{"id":996278,"date":"2024-12-27T09:12:22","date_gmt":"2024-12-27T01:12:22","guid":{"rendered":""},"modified":"2024-12-27T09:12:24","modified_gmt":"2024-12-27T01:12:24","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8ia%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/996278.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528ia\u63a5\u53e3"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072610\/11a636c2-9543-4430-b4c8-e06073cf8fda.webp\" alt=\"python\u5982\u4f55\u8c03\u7528ia\u63a5\u53e3\" \/><\/p>\n<p><p> <strong>Python\u8c03\u7528IA\u63a5\u53e3\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u5b89\u88c5\u76f8\u5173\u5e93\u3001\u83b7\u53d6API\u5bc6\u94a5\u3001\u53d1\u9001\u8bf7\u6c42\u3001\u5904\u7406\u54cd\u5e94\u3001\u9519\u8bef\u5904\u7406<\/strong>\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Python\u8c03\u7528IA\u63a5\u53e3\uff0c\u786e\u4fdd\u60a8\u80fd\u591f\u987a\u5229\u5b8c\u6210\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u76f8\u5173\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u8c03\u7528IA\u63a5\u53e3\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Python\u7684\u76f8\u5173\u5e93\uff0c\u5982requests\u548cjson\u3002\u8fd9\u4e9b\u5e93\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u5904\u7406HTTP\u8bf7\u6c42\u548c\u54cd\u5e94\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5requests\u5e93<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>requests\u5e93\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684HTTP\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u53d1\u9001HTTP\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5b89\u88c5json\u5e93<\/p>\n<p>json\u5e93\u662fPython\u5185\u7f6e\u7684\u5e93\uff0c\u7528\u4e8e\u5904\u7406JSON\u683c\u5f0f\u7684\u6570\u636e\u3002<\/li>\n<\/p>\n<\/ol>\n<p><p>\u4e8c\u3001\u83b7\u53d6API\u5bc6\u94a5<\/p>\n<\/p>\n<p><p>API\u5bc6\u94a5\u662f\u8c03\u7528IA\u63a5\u53e3\u7684\u5fc5\u8981\u51ed\u8bc1\u3002\u5728\u4f7f\u7528\u67d0\u4e2aIA\u670d\u52a1\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u5728\u5176\u5b98\u65b9\u7f51\u7ad9\u6ce8\u518c\u5e76\u83b7\u53d6API\u5bc6\u94a5\u3002\u901a\u5e38\uff0c\u60a8\u9700\u8981\u767b\u5f55\u5230\u5f00\u53d1\u8005\u63a7\u5236\u53f0\uff0c\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\uff0c\u5e76\u751f\u6210\u76f8\u5e94\u7684API\u5bc6\u94a5\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u53d1\u9001\u8bf7\u6c42<\/p>\n<\/p>\n<p><p>\u83b7\u53d6API\u5bc6\u94a5\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u5411IA\u63a5\u53e3\u53d1\u9001\u8bf7\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u8bf7\u6c42\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &quot;https:\/\/api.example.com\/endpoint&quot;<\/p>\n<p>headers = {<\/p>\n<p>    &quot;Authorization&quot;: &quot;Bearer YOUR_API_KEY&quot;,<\/p>\n<p>    &quot;Content-Type&quot;: &quot;application\/json&quot;<\/p>\n<p>}<\/p>\n<p>response = requests.get(url, headers=headers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u54cd\u5e94<\/p>\n<\/p>\n<p><p>\u8bf7\u6c42\u6210\u529f\u540e\uff0cIA\u63a5\u53e3\u5c06\u8fd4\u56de\u4e00\u4e2a\u54cd\u5e94\u3002\u901a\u5e38\uff0c\u54cd\u5e94\u4ee5JSON\u683c\u5f0f\u8fd4\u56de\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528json\u5e93\u5bf9\u5176\u8fdb\u884c\u89e3\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    data = json.loads(response.text)<\/p>\n<p>    # \u5904\u7406data\u4e2d\u7684\u4fe1\u606f<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801\uff1a&quot;, response.status_code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u9519\u8bef\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u8c03\u7528IA\u63a5\u53e3\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u5982\u7f51\u7edc\u9519\u8bef\u3001\u63a5\u53e3\u9519\u8bef\u3001\u6743\u9650\u9519\u8bef\u7b49\u3002\u56e0\u6b64\uff0c\u8fdb\u884c\u9519\u8bef\u5904\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<ol>\n<li>\u7f51\u7edc\u9519\u8bef<\/p>\n<p>\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f51\u7edc\u8fde\u63a5\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    response = requests.get(url, headers=headers)<\/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.RequestException as e:<\/p>\n<p>    print(&quot;\u7f51\u7edc\u9519\u8bef\uff1a&quot;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u63a5\u53e3\u9519\u8bef<\/p>\n<p>\u63a5\u53e3\u9519\u8bef\u901a\u5e38\u7531\u65e0\u6548\u7684\u8bf7\u6c42\u53c2\u6570\u6216\u670d\u52a1\u5668\u95ee\u9898\u5f15\u8d77\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u54cd\u5e94\u4e2d\u7684\u9519\u8bef\u4fe1\u606f\u6765\u8bca\u65ad\u95ee\u9898\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">if response.status_code != 200:<\/p>\n<p>    error_info = json.loads(response.text)<\/p>\n<p>    print(&quot;\u63a5\u53e3\u9519\u8bef\uff1a&quot;, error_info[&quot;error_message&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u6743\u9650\u9519\u8bef<\/p>\n<p>\u6743\u9650\u9519\u8bef\u901a\u5e38\u7531\u65e0\u6548\u7684API\u5bc6\u94a5\u6216\u6743\u9650\u4e0d\u8db3\u5f15\u8d77\u3002\u786e\u4fdd\u60a8\u7684API\u5bc6\u94a5\u6b63\u786e\uff0c\u5e76\u68c0\u67e5\u662f\u5426\u6709\u8db3\u591f\u7684\u6743\u9650\u8bbf\u95ee\u6240\u9700\u7684\u8d44\u6e90\u3002<\/li>\n<\/p>\n<\/ol>\n<p><p>\u516d\u3001\u5b9e\u4f8b\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5728\u4e86\u89e3\u4e86\u5982\u4f55\u8c03\u7528IA\u63a5\u53e3\u7684\u57fa\u672c\u6b65\u9aa4\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u5b9e\u4f8b\u6765\u6f14\u793a\u5982\u4f55\u4f7f\u7528Python\u8c03\u7528IA\u63a5\u53e3\u3002\u5047\u8bbe\u6211\u4eec\u8981\u8c03\u7528\u4e00\u4e2a\u5929\u6c14\u670d\u52a1\u7684IA\u63a5\u53e3\uff0c\u83b7\u53d6\u67d0\u4e2a\u57ce\u5e02\u7684\u5929\u6c14\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\n<p>\u83b7\u53d6API\u5bc6\u94a5<\/p>\n<p>\u5728\u5929\u6c14\u670d\u52a1\u7684\u5b98\u65b9\u7f51\u7ad9\u6ce8\u518c\u5e76\u83b7\u53d6API\u5bc6\u94a5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u53d1\u9001\u8bf7\u6c42<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def get_weather(city):<\/p>\n<p>    url = f&quot;http:\/\/api.weatherapi.com\/v1\/current.json?key=YOUR_API_KEY&amp;q={city}&quot;<\/p>\n<p>    headers = {<\/p>\n<p>        &quot;Content-Type&quot;: &quot;application\/json&quot;<\/p>\n<p>    }<\/p>\n<p>    try:<\/p>\n<p>        response = requests.get(url, headers=headers)<\/p>\n<p>        response.raise_for_status()<\/p>\n<p>        if response.status_code == 200:<\/p>\n<p>            data = response.json()<\/p>\n<p>            return data<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;\u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801\uff1a&quot;, response.status_code)<\/p>\n<p>            return None<\/p>\n<p>    except requests.exceptions.RequestException as e:<\/p>\n<p>        print(&quot;\u7f51\u7edc\u9519\u8bef\uff1a&quot;, e)<\/p>\n<p>        return None<\/p>\n<p>city = &quot;London&quot;<\/p>\n<p>weather_data = get_weather(city)<\/p>\n<p>if weather_data:<\/p>\n<p>    print(f&quot;{city}\u7684\u5929\u6c14\uff1a&quot;)<\/p>\n<p>    print(&quot;\u6e29\u5ea6\uff1a&quot;, weather_data[&#39;current&#39;][&#39;temp_c&#39;], &quot;\u00b0C&quot;)<\/p>\n<p>    print(&quot;\u5929\u6c14\uff1a&quot;, weather_data[&#39;current&#39;][&#39;condition&#39;][&#39;text&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u80fd\u591f\u83b7\u53d6\u5e76\u6253\u5370\u51fa\u6307\u5b9a\u57ce\u5e02\u7684\u5929\u6c14\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u8c03\u7528IA\u63a5\u53e3\u7684\u6d41\u7a0b\u5305\u62ec\uff1a\u5b89\u88c5\u76f8\u5173\u5e93\u3001\u83b7\u53d6API\u5bc6\u94a5\u3001\u53d1\u9001\u8bf7\u6c42\u3001\u5904\u7406\u54cd\u5e94\u548c\u9519\u8bef\u5904\u7406\u3002\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u8c03\u7528IA\u63a5\u53e3\u83b7\u53d6\u6240\u9700\u6570\u636e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u6839\u636e\u5177\u4f53\u7684IA\u63a5\u53e3\u6587\u6863\u8fdb\u884c\u8c03\u6574\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u63a5\u53e3\u7684\u8981\u6c42\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u4e3a\u60a8\u63d0\u4f9b\u5e2e\u52a9\uff0c\u4f7f\u60a8\u80fd\u591f\u987a\u5229\u5b8c\u6210Python\u8c03\u7528IA\u63a5\u53e3\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8c03\u7528IA\u63a5\u53e3\uff1f<\/strong><br \/>Python\u8c03\u7528IA\u63a5\u53e3\u7684\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\u5b89\u88c5\u76f8\u5173\u5e93\u3001\u83b7\u53d6API\u5bc6\u94a5\u3001\u7f16\u5199\u8bf7\u6c42\u4ee3\u7801\u548c\u5904\u7406\u54cd\u5e94\u6570\u636e\u3002\u63a8\u8350\u4f7f\u7528<code>requests<\/code>\u5e93\u6765\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86\u5b83\u3002\u83b7\u53d6API\u5bc6\u94a5\u901a\u5e38\u9700\u8981\u5728IA\u5e73\u53f0\u6ce8\u518c\uff0c\u5e76\u5728\u7528\u6237\u4e2d\u5fc3\u627e\u5230\u76f8\u5e94\u7684\u5bc6\u94a5\u3002\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u901a\u5e38\u9700\u8981\u8bbe\u7f6e\u8bf7\u6c42\u7684URL\u3001\u8bf7\u6c42\u65b9\u6cd5\uff08\u5982GET\u6216POST\uff09\u3001\u8bf7\u6c42\u5934\u548c\u8bf7\u6c42\u4f53\uff0c\u5e76\u6839\u636eAPI\u6587\u6863\u89e3\u6790\u8fd4\u56de\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5728\u8c03\u7528IA\u63a5\u53e3\u65f6\uff0c\u5982\u4f55\u5904\u7406\u8bf7\u6c42\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u8c03\u7528IA\u63a5\u53e3\u65f6\uff0c\u9519\u8bef\u5904\u7406\u81f3\u5173\u91cd\u8981\u3002\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u6765\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u7f51\u7edc\u9519\u8bef\u6216API\u8fd4\u56de\u7684\u9519\u8bef\u72b6\u6001\u7801\u3002\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u8bf7\u6c42\u8fc7\u7a0b\u4e2d\u7684\u5f02\u5e38\uff0c\u68c0\u67e5\u8fd4\u56de\u7684\u72b6\u6001\u7801\u662f\u5426\u4e3a200\uff08\u6210\u529f\uff09\uff0c\u5982\u679c\u4e0d\u662f\uff0c\u53ef\u4ee5\u6839\u636e\u8fd4\u56de\u7684\u9519\u8bef\u4fe1\u606f\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\uff0c\u6bd4\u5982\u8bb0\u5f55\u65e5\u5fd7\u3001\u91cd\u8bd5\u8bf7\u6c42\u6216\u63d0\u793a\u7528\u6237\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u89c1\u7684IA\u63a5\u53e3\u53ef\u4ee5\u4f7f\u7528\uff1f<\/strong><br \/>\u5e38\u89c1\u7684IA\u63a5\u53e3\u5305\u62ec\u81ea\u7136\u8bed\u8a00\u5904\u7406\u3001\u56fe\u50cf\u8bc6\u522b\u3001\u8bed\u97f3\u8bc6\u522b\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b49\u3002\u4f8b\u5982\uff0cGoogle Cloud\u7684\u81ea\u7136\u8bed\u8a00API\u3001IBM Watson\u7684\u89c6\u89c9\u8bc6\u522bAPI\u548cOpenAI\u7684GPT\u63a5\u53e3\u7b49\u3002\u8fd9\u4e9b\u63a5\u53e3\u63d0\u4f9b\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u5e94\u7528\u4e2d\u5b9e\u73b0\u667a\u80fd\u5316\u7684\u5904\u7406\u3002\u9009\u62e9\u5408\u9002\u7684\u63a5\u53e3\u65f6\uff0c\u5efa\u8bae\u67e5\u770b\u5404\u81ea\u7684\u6587\u6863\uff0c\u4e86\u89e3\u5176\u529f\u80fd\u3001\u9650\u5236\u548c\u8d39\u7528\u7ed3\u6784\uff0c\u4ece\u800c\u505a\u51fa\u660e\u667a\u7684\u9009\u62e9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8c03\u7528IA\u63a5\u53e3\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u5b89\u88c5\u76f8\u5173\u5e93\u3001\u83b7\u53d6API\u5bc6\u94a5\u3001\u53d1\u9001\u8bf7\u6c42\u3001\u5904\u7406\u54cd\u5e94\u3001\u9519\u8bef\u5904\u7406\u3002\u6211\u4eec\u5c06\u8be6\u7ec6 [&hellip;]","protected":false},"author":3,"featured_media":996280,"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\/996278"}],"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=996278"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/996278\/revisions"}],"predecessor-version":[{"id":996281,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/996278\/revisions\/996281"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/996280"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=996278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=996278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=996278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}