{"id":1105528,"date":"2025-01-08T16:31:45","date_gmt":"2025-01-08T08:31:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1105528.html"},"modified":"2025-01-08T16:31:48","modified_gmt":"2025-01-08T08:31:48","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8python%e7%88%ac%e5%8f%96%e4%bb%8a%e5%a4%a9%e5%a4%a9%e6%b0%94","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1105528.html","title":{"rendered":"\u5982\u4f55\u5229\u7528Python\u722c\u53d6\u4eca\u5929\u5929\u6c14"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070412\/98eb53d7-73df-4b29-bf42-08cfda19c5fe.webp\" alt=\"\u5982\u4f55\u5229\u7528Python\u722c\u53d6\u4eca\u5929\u5929\u6c14\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u5229\u7528Python\u722c\u53d6\u4eca\u5929\u5929\u6c14<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u5229\u7528Python\u722c\u53d6\u4eca\u5929\u5929\u6c14\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u9009\u62e9\u5408\u9002\u7684API\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3001\u7f16\u5199\u722c\u866b\u4ee3\u7801\u3001\u5904\u7406\u548c\u5c55\u793a\u6570\u636e\u3002<strong>\u9009\u62e9\u5408\u9002\u7684API\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3001\u7f16\u5199\u722c\u866b\u4ee3\u7801\u3001\u5904\u7406\u548c\u5c55\u793a\u6570\u636e<\/strong>\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u9009\u62e9\u4e00\u4e2a\u63d0\u4f9b\u5929\u6c14\u6570\u636e\u7684API\u670d\u52a1\uff0c\u4f8b\u5982OpenWeatherMap\u6216WeatherAPI\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5929\u6c14\u6570\u636e\u63a5\u53e3\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Python\u7684requests\u5e93\u6765\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u5e76\u4f7f\u7528json\u5e93\u6765\u89e3\u6790\u6570\u636e\u3002\u7136\u540e\uff0c\u6211\u4eec\u7f16\u5199\u4ee3\u7801\u6765\u4eceAPI\u83b7\u53d6\u5929\u6c14\u6570\u636e\uff0c\u6700\u540e\u5904\u7406\u5e76\u5c55\u793a\u8fd9\u4e9b\u6570\u636e\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><p><strong>\u9009\u62e9\u5408\u9002\u7684API<\/strong><\/p>\n<\/p>\n<p><p>\u9009\u62e9\u4e00\u4e2a\u597d\u7684API\u662f\u6210\u529f\u722c\u53d6\u5929\u6c14\u6570\u636e\u7684\u5173\u952e\u3002OpenWeatherMap\u548cWeatherAPI\u662f\u4e24\u4e2a\u975e\u5e38\u6d41\u884c\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u5929\u6c14\u6570\u636eAPI\u3002\u5b83\u4eec\u63d0\u4f9b\u4e86\u5f53\u524d\u5929\u6c14\u3001\u9884\u62a5\u3001\u5386\u53f2\u6570\u636e\u7b49\u4e30\u5bcc\u7684\u63a5\u53e3\uff0c\u5e76\u4e14\u6709\u826f\u597d\u7684\u6587\u6863\u652f\u6301\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>OpenWeatherMap<\/strong>\uff1a\u8fd9\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u5929\u6c14\u6570\u636eAPI\uff0c\u5b83\u63d0\u4f9b\u4e86\u514d\u8d39\u7684\u548c\u4ed8\u8d39\u7684\u670d\u52a1\u3002\u514d\u8d39\u670d\u52a1\u7684\u9650\u5236\u662f\u6bcf\u5206\u949f\u8c03\u7528\u6b21\u6570\u53d7\u9650\uff0c\u5e76\u4e14\u6570\u636e\u6709\u4e00\u5b9a\u7684\u5ef6\u8fdf\u3002<\/li>\n<li><strong>WeatherAPI<\/strong>\uff1a\u8fd9\u4e2aAPI\u4e5f\u63d0\u4f9b\u4e86\u514d\u8d39\u7684\u548c\u4ed8\u8d39\u7684\u670d\u52a1\u3002\u5b83\u7684\u6570\u636e\u66f4\u52a0\u5b9e\u65f6\uff0c\u63a5\u53e3\u66f4\u52a0\u7b80\u6d01\uff0c\u9002\u5408\u5feb\u901f\u5f00\u53d1\u3002<\/li>\n<\/ol>\n<p><p><strong>\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u5fc5\u8981\u7684\u5e93\u3002requests\u5e93\u662f\u7528\u6765\u53d1\u9001HTTP\u8bf7\u6c42\u7684\uff0cjson\u5e93\u662f\u7528\u6765\u89e3\u6790JSON\u6570\u636e\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u7f16\u5199\u722c\u866b\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5f00\u59cb\u7f16\u5199\u722c\u866b\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528OpenWeatherMap API\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>import json<\/p>\n<p>def get_weather(city):<\/p>\n<p>    api_key = &quot;your_api_key_here&quot;<\/p>\n<p>    base_url = &quot;http:\/\/api.openweathermap.org\/data\/2.5\/weather?&quot;<\/p>\n<p>    complete_url = base_url + &quot;q=&quot; + city + &quot;&amp;appid=&quot; + api_key<\/p>\n<p>    response = requests.get(complete_url)<\/p>\n<p>    data = response.json()<\/p>\n<p>    if data[&quot;cod&quot;] != &quot;404&quot;:<\/p>\n<p>        m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n = data[&quot;main&quot;]<\/p>\n<p>        weather = data[&quot;weather&quot;][0]<\/p>\n<p>        temp = main[&quot;temp&quot;]<\/p>\n<p>        pressure = main[&quot;pressure&quot;]<\/p>\n<p>        humidity = main[&quot;humidity&quot;]<\/p>\n<p>        description = weather[&quot;description&quot;]<\/p>\n<p>        weather_info = {<\/p>\n<p>            &quot;Temperature&quot;: temp,<\/p>\n<p>            &quot;Pressure&quot;: pressure,<\/p>\n<p>            &quot;Humidity&quot;: humidity,<\/p>\n<p>            &quot;Description&quot;: description<\/p>\n<p>        }<\/p>\n<p>        return weather_info<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<p>city = &quot;London&quot;<\/p>\n<p>weather_info = get_weather(city)<\/p>\n<p>if weather_info:<\/p>\n<p>    print(&quot;Temperature: &quot; + str(weather_info[&quot;Temperature&quot;]))<\/p>\n<p>    print(&quot;Pressure: &quot; + str(weather_info[&quot;Pressure&quot;]))<\/p>\n<p>    print(&quot;Humidity: &quot; + str(weather_info[&quot;Humidity&quot;]))<\/p>\n<p>    print(&quot;Description: &quot; + weather_info[&quot;Description&quot;])<\/p>\n<p>else:<\/p>\n<p>    print(&quot;City Not Found!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570get_weather\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u57ce\u5e02\u540d\u79f0\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u8be5\u57ce\u5e02\u7684\u5929\u6c14\u4fe1\u606f\u3002\u6211\u4eec\u4f7f\u7528requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u5e76\u4f7f\u7528json\u5e93\u89e3\u6790\u8fd4\u56de\u7684\u6570\u636e\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06\u5929\u6c14\u4fe1\u606f\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><p><strong>\u5904\u7406\u548c\u5c55\u793a\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u83b7\u53d6\u5230\u5929\u6c14\u6570\u636e\u540e\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u5c55\u793a\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\uff0c\u6216\u8005\u901a\u8fc7\u7f51\u9875\u3001\u547d\u4ee4\u884c\u754c\u9762\u7b49\u65b9\u5f0f\u5c55\u793a\u51fa\u6765\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5c55\u793a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def display_weather_info(weather_info):<\/p>\n<p>    print(&quot;Temperature: &quot; + str(weather_info[&quot;Temperature&quot;]) + &quot; K&quot;)<\/p>\n<p>    print(&quot;Pressure: &quot; + str(weather_info[&quot;Pressure&quot;]) + &quot; hPa&quot;)<\/p>\n<p>    print(&quot;Humidity: &quot; + str(weather_info[&quot;Humidity&quot;]) + &quot; %&quot;)<\/p>\n<p>    print(&quot;Description: &quot; + weather_info[&quot;Description&quot;])<\/p>\n<p>if weather_info:<\/p>\n<p>    display_weather_info(weather_info)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;City Not Found!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570display_weather_info\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5929\u6c14\u4fe1\u606f\u5b57\u5178\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u4fe1\u606f\u6253\u5370\u51fa\u6765\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u548c\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><p><strong>\u5c0f\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u6210\u529f\u5730\u5229\u7528Python\u722c\u53d6\u4eca\u5929\u5929\u6c14\u3002\u9009\u62e9\u5408\u9002\u7684API\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3001\u7f16\u5199\u722c\u866b\u4ee3\u7801\u3001\u5904\u7406\u548c\u5c55\u793a\u6570\u636e\u662f\u5173\u952e\u6b65\u9aa4\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6\u5929\u6c14\u6570\u636e\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684requests\u5e93\u6765\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u4ece\u5929\u6c14API\u83b7\u53d6\u5929\u6c14\u6570\u636e\u3002\u5e38\u7528\u7684\u5929\u6c14API\u5305\u62ecOpenWeatherMap\u548cWeather API\u7b49\u3002\u83b7\u53d6\u5230\u7684\u6570\u636e\u901a\u5e38\u662fJSON\u683c\u5f0f\uff0c\u901a\u8fc7Python\u7684json\u5e93\u8fdb\u884c\u89e3\u6790\u540e\uff0c\u5c31\u53ef\u4ee5\u63d0\u53d6\u51fa\u6240\u9700\u7684\u5929\u6c14\u4fe1\u606f\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u722c\u866b\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u722c\u53d6\u5929\u6c14\u6570\u636e\u65f6\uff0c\u9700\u9075\u5faa\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\u89c4\u5b9a\uff0c\u4ee5\u786e\u4fdd\u9075\u5b88\u7f51\u7ad9\u7684\u722c\u866b\u653f\u7b56\u3002\u6b64\u5916\uff0c\u5408\u7406\u8bbe\u7f6e\u8bf7\u6c42\u95f4\u9694\u65f6\u95f4\uff0c\u9632\u6b62\u5bf9\u670d\u52a1\u5668\u9020\u6210\u8fc7\u5927\u538b\u529b\uff0c\u907f\u514d\u88ab\u5c01IP\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u7528\u54ea\u4e9bPython\u5e93\u6765\u8f85\u52a9\u722c\u53d6\u5929\u6c14\u4fe1\u606f\uff1f<\/strong><br \/>\u9664\u4e86requests\u5e93\u5916\uff0cBeautiful Soup\u548clxml\u662f\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684\u5e38\u7528\u5e93\uff0c\u9002\u5408\u5904\u7406\u4ece\u7f51\u7ad9\u4e0a\u76f4\u63a5\u6293\u53d6\u7684\u5929\u6c14\u4fe1\u606f\u3002\u5982\u679c\u4f7f\u7528API\u83b7\u53d6\u5929\u6c14\u6570\u636e\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528json\u5e93\u6765\u5904\u7406\u54cd\u5e94\u6570\u636e\u3002\u5176\u4ed6\u5982pandas\u5e93\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u5229\u7528Python\u722c\u53d6\u4eca\u5929\u5929\u6c14 \u8981\u5229\u7528Python\u722c\u53d6\u4eca\u5929\u5929\u6c14\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u9009\u62e9\u5408\u9002\u7684API\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1105533,"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\/1105528"}],"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=1105528"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105528\/revisions"}],"predecessor-version":[{"id":1105535,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105528\/revisions\/1105535"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1105533"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1105528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1105528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1105528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}