{"id":2640,"date":"2023-05-04T15:30:19","date_gmt":"2023-05-04T07:30:19","guid":{"rendered":"https:\/\/benson.ml\/?p=2640"},"modified":"2023-05-04T15:55:40","modified_gmt":"2023-05-04T07:55:40","slug":"%e7%94%a8python%e7%bc%96%e5%86%99%e4%b8%80%e4%b8%aa%e5%ae%9e%e6%97%b6%e5%a4%a9%e6%b0%94%e7%9a%84%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/benson80.eu.org\/?p=2640","title":{"rendered":"\u7528Python\u7f16\u5199\u4e00\u4e2a\u5b9e\u65f6\u5929\u6c14\u7684\u7a0b\u5e8f"},"content":{"rendered":"\n<p>\u9996\u5148\uff0c\u9700\u8981\u6ce8\u518c\u4e00\u4e2a<strong>\u548c\u98ce\u5929\u6c14<\/strong>\u7684\u8d26\u53f7\uff0c\u5e76\u4e14\u83b7\u5f97\u4e00\u4e2aAPI Key\u3002<a rel=\"noreferrer noopener\" href=\"https:\/\/console.qweather.com\" target=\"_blank\">https:\/\/console.qweather.com<\/a>\u5728\u6ce8\u518c\u6210\u529f\u5e76\u83b7\u5f97API Key\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Python\u4e2d\u7684requests\u5e93\u5411\u548c\u98ce\u5929\u6c14API\u53d1\u9001\u8bf7\u6c42\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"269\" src=\"https:\/\/benson.ml\/wp-content\/uploads\/2023\/05\/image-1024x269.png\" alt=\"\" class=\"wp-image-2642\" srcset=\"https:\/\/benson80.eu.org\/wp-content\/uploads\/2023\/05\/image-1024x269.png 1024w, https:\/\/benson80.eu.org\/wp-content\/uploads\/2023\/05\/image-300x79.png 300w, https:\/\/benson80.eu.org\/wp-content\/uploads\/2023\/05\/image-768x201.png 768w, https:\/\/benson80.eu.org\/wp-content\/uploads\/2023\/05\/image-421x110.png 421w, https:\/\/benson80.eu.org\/wp-content\/uploads\/2023\/05\/image.png 1136w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u4f7f\u7528input()\u51fd\u6570\u6765\u63d0\u793a\u7528\u6237\u8f93\u5165\u57ce\u5e02\u540d\u79f0\uff0c\u5e76\u5c06\u7528\u6237\u8f93\u5165\u7684\u57ce\u5e02\u540d\u79f0\u4f5c\u4e3a\u8bf7\u6c42URL\u7684\u53c2\u6570\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u5148\u6839\u636e\u57ce\u5e02\u540d\u79f0\u83b7\u53d6\u57ce\u5e02ID\uff0c\u7136\u540e\u5c06\u57ce\u5e02ID\u7528\u4e8e\u67e5\u8be2\u5b9e\u65f6\u5929\u6c14\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u8f93\u51fa\u4e86\u57ce\u5e02\u540d\u79f0\u548c\u6240\u67e5\u8be2\u57ce\u5e02\u7684\u5929\u6c14\u4fe1\u606f\u3002<\/p>\n\n\n\n<p><strong>\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\nimport time\n\napi_key = \"xxxxxxxxxxxxxxxxxxxx\"\ncity_name = input(\"\u8bf7\u8f93\u5165\u57ce\u5e02\u540d\u79f0\uff1a\")\n\n# \u901a\u8fc7\u57ce\u5e02\u67e5\u8be2\u63a5\u53e3\u83b7\u53d6\u57ce\u5e02ID\ncity_url = f\"https:\/\/geoapi.qweather.com\/v2\/city\/lookup?key={api_key}&amp;location={city_name}&amp;lang=zh\"\n\ncity_response = requests.get(city_url)\ncity_data = city_response.json()\n\nif city_data&#91;\"code\"] == \"200\":\n    city_id = city_data&#91;\"location\"]&#91;0]&#91;\"id\"]\n    print(f\"\u57ce\u5e02 {city_name} \u5bf9\u5e94\u7684\u57ce\u5e02ID \u4e3a\uff1a{city_id}\")\n    \n    # \u4f7f\u7528\u57ce\u5e02ID\u67e5\u8be2\u5b9e\u65f6\u5929\u6c14\u4fe1\u606f\n    weather_url = f\"https:\/\/devapi.qweather.com\/v7\/weather\/now?key={api_key}&amp;location={city_id}\"\n    weather_response = requests.get(weather_url)\n    weather_data = weather_response.json()\n\n    if weather_data&#91;\"code\"] == \"200\":\n        now = weather_data&#91;\"now\"]\n        print(f\"{city_name}\u7684\u5929\u6c14\u60c5\u51b5\uff1a\")\n        print(f\"\u5929\u6c14\u72b6\u51b5\uff1a{now&#91;'text']}\")\n        print(f\"\u6e29\u5ea6\uff1a{now&#91;'temp']}\u2103\")\n        print(f\"\u4f53\u611f\u6e29\u5ea6\uff1a{now&#91;'feelsLike']}\u2103\")\n        print(f\"\u98ce\u5411\uff1a{now&#91;'windDir']}\")\n        print(f\"\u98ce\u901f\uff1a{now&#91;'windSpeed']}km\/h\")\n    else:\n        print(\"\u83b7\u53d6\u5929\u6c14\u4fe1\u606f\u5931\u8d25\uff01\")\nelse:\n    print(\"\u83b7\u53d6\u57ce\u5e02\u4fe1\u606f\u5931\u8d25\uff01\")\ntime.sleep(5)<\/code><\/pre>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684 Python \u5b9e\u73b0\uff0c\u5b83\u4f7f\u7528 OpenWeatherMap API \u83b7\u53d6\u5f53\u524d\u57ce\u5e02\u7684\u5929\u6c14\u6570\u636e\uff0c\u5e76\u5728\u63a7\u5236\u53f0\u4e2d\u663e\u793a\u6e29\u5ea6\u3001\u6e7f\u5ea6\u548c\u5929\u6c14\u72b6\u51b5\uff1a<\/p>\n\n\n\n<p>import requests<\/p>\n\n\n\n<p>api_key = &#8220;xxxxxxxxxxxxxxxxxxxx&#8221; # \u5728OpenWeatherMap\u7f51\u7ad9\u6ce8\u518cAPI key<\/p>\n\n\n\n<p>city = input(&#8220;\u8bf7\u8f93\u5165\u57ce\u5e02\u540d\u79f0\uff1a&#8221;)<\/p>\n\n\n\n<p>url = f&#8221;http:\/\/api.openweathermap.org\/data\/2.5\/weather?q={city}&amp;appid={api_key}&amp;units=metric&#8221;<\/p>\n\n\n\n<p>response = requests.get(url)<\/p>\n\n\n\n<p>if response.status_code == 200:<br>data = response.json()<br>print(f&#8221;\u57ce\u5e02: {data[&#8216;name&#8217;]}&#8221;)<br>print(f&#8221;\u6e29\u5ea6: {data[&#8216;main&#8217;][&#8216;temp&#8217;]}\u00b0C&#8221;)<br>print(f&#8221;\u6e7f\u5ea6: {data[&#8216;main&#8217;][&#8216;humidity&#8217;]}%&#8221;)<br>print(f&#8221;\u5929\u6c14\u72b6\u51b5: {data[&#8216;weather&#8217;][0][&#8216;description&#8217;]}&#8221;)<br>else:<br>print(&#8220;\u83b7\u53d6\u5929\u6c14\u4fe1\u606f\u5931\u8d25&#8221;)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"496\" height=\"478\" src=\"https:\/\/benson.ml\/wp-content\/uploads\/2023\/05\/image-1.png\" alt=\"\" class=\"wp-image-2654\" srcset=\"https:\/\/benson80.eu.org\/wp-content\/uploads\/2023\/05\/image-1.png 496w, https:\/\/benson80.eu.org\/wp-content\/uploads\/2023\/05\/image-1-300x289.png 300w, https:\/\/benson80.eu.org\/wp-content\/uploads\/2023\/05\/image-1-311x300.png 311w\" sizes=\"auto, (max-width: 496px) 100vw, 496px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\uff0c\u9700\u8981\u6ce8\u518c\u4e00\u4e2a\u548c\u98ce\u5929\u6c14\u7684\u8d26\u53f7\uff0c\u5e76\u4e14\u83b7\u5f97\u4e00\u4e2aAP &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-2640","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/2640","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2640"}],"version-history":[{"count":11,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/2640\/revisions"}],"predecessor-version":[{"id":2656,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/2640\/revisions\/2656"}],"wp:attachment":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}