{"id":121,"date":"2026-08-06T00:31:29","date_gmt":"2026-08-05T16:31:29","guid":{"rendered":"https:\/\/ltrainyard.icu\/?p=121"},"modified":"2026-08-06T00:46:20","modified_gmt":"2026-08-05T16:46:20","slug":"day-27-web-api%ef%bc%9ahttp-rest-get-post-%e4%b8%8e-json-%e8%b0%83%e7%94%a8","status":"publish","type":"post","link":"https:\/\/ltrainyard.icu\/index.php\/2026\/08\/06\/day-27-web-api%ef%bc%9ahttp-rest-get-post-%e4%b8%8e-json-%e8%b0%83%e7%94%a8\/","title":{"rendered":"| Day 27 | Web API\uff1aHTTP \/ REST \/ GET \/ POST \u4e0e JSON \u8c03\u7528 |"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1 HTTP \u4e0e REST \u2014\u2014 \u7a0b\u5e8f\u4e4b\u95f4\u7684\u300c\u666e\u901a\u8bdd\u300d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>HTTP<\/strong>\uff08\u8d85\u6587\u672c\u4f20\u8f93\u534f\u8bae\uff09\u662f\u7a0b\u5e8f\u4e4b\u95f4\u901a\u4fe1\u7684\u901a\u7528\u8bed\u8a00\uff1b<strong>REST<\/strong> \u662f\u8bbe\u8ba1 API \u7684\u98ce\u683c\u7ea6\u5b9a\u2014\u2014\u628a\u670d\u52a1\u5668\u4e0a\u7684\u8d44\u6e90\uff08\u5929\u6c14\u3001\u7528\u6237\u3001\u8ba2\u5355\uff09\u7528 URL \u8868\u793a\uff0c\u7528 HTTP \u65b9\u6cd5\u8868\u793a\u64cd\u4f5c\u3002\u770b\u61c2\u8fd9\u4e24\u4e2a\u6982\u5ff5\uff0c\u4efb\u4f55 API \u6587\u6863\u5728\u4f60\u9762\u524d\u90fd\u662f\u300c\u6253\u5f00\u5373\u7528\u300d\u3002\u6628\u5929\u722c\u7f51\u9875\u62ff\u7684\u662f HTML\u2014\u2014\u4e00\u5806\u7ed9\u4eba\u770b\u7684\u6807\u7b7e\uff1b\u4eca\u5929\u6362\u4e00\u79cd\u601d\u8def\uff1a<strong>API\uff08\u63a5\u53e3\uff09\u662f\u4e13\u95e8\u7ed9\u7a0b\u5e8f\u7528\u7684\u300c\u670d\u52a1\u300d<\/strong>\uff0c\u670d\u52a1\u5668\u76f4\u63a5\u8fd4\u56de<strong>\u7ed3\u6784\u5316\u6570\u636e<\/strong>\uff08\u901a\u5e38\u662f JSON\uff09\uff0c\u4e0d\u7528\u518d\u8d39\u52b2\u4ece HTML \u91cc\u6311\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">HTTP \u65b9\u6cd5\u89c4\u5b9a\u8bf7\u6c42\u300c\u60f3\u5e72\u4ec0\u4e48\u300d\uff1a<code>GET<\/code> \u8981\u6570\u636e\uff08\u8bfb\uff0c\u53c2\u6570\u653e URL \u91cc\uff09\uff0c<code>POST<\/code> \u63d0\u4ea4\u6570\u636e\uff08\u5199\uff0c\u6570\u636e\u653e\u8bf7\u6c42\u4f53\u91cc\uff09\uff0c\u8fd8\u6709 <code>PUT<\/code>\uff08\u6574\u4f53\u66f4\u65b0\uff09\u3001<code>DELETE<\/code>\uff08\u5220\u9664\uff09\u3002URL \u7ed3\u6784\uff1a<code>\u534f\u8bae:\/\/\u57df\u540d\/\u8def\u5f84?\u952e=\u503c&amp;\u952e=\u503c<\/code>\u3002REST \u98ce\u683c\u4e0b\uff0c<code>GET \/v1\/forecast<\/code> \u5c31\u662f\u300c\u8bfb\u9884\u62a5\u8d44\u6e90\u300d\u3002<strong>GET \u53c2\u6570\u4f1a\u66b4\u9732\u5728 URL \u91cc\uff0c\u522b\u653e\u5bc6\u7801\uff1bPOST \u6570\u636e\u5728\u8bf7\u6c42\u4f53\u91cc\u76f8\u5bf9\u5b89\u5168<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u50cf<strong>\u9910\u5385\u70b9\u9910<\/strong>\uff1a\u83dc\u540d\u662f\u8d44\u6e90\uff08URL\uff09\uff0c\u300c\u6765\u4e00\u4efd\u300d\u662f GET\uff08\u53ea\u8bfb\uff09\u3001\u300c\u52a0\u4e00\u4efd\u7279\u5236\u300d\u662f POST\uff08\u63d0\u4ea4\u6570\u636e\uff09\u3001\u300c\u9000\u6389\u300d\u662f DELETE\u3002\u4f60\u8bf4\u7684\u8bdd\uff08HTTP \u65b9\u6cd5\uff09\u51b3\u5b9a\u4e86\u670d\u52a1\u5458\uff08\u670d\u52a1\u5668\uff09\u600e\u4e48\u5904\u7406\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u6d4f\u89c8\u5668\u8bbf\u95ee URL \u5c31\u662f\u4e00\u6b21 GET \u8bf7\u6c42<\/strong>\uff08\u9700\u8981\u8054\u7f51\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\n\nurl = \"https:\/\/api.open-meteo.com\/v1\/forecast?latitude=39.9042&amp;longitude=116.4074\"\nresp = requests.get(url, timeout=10)\nprint(\"\u72b6\u6001\u7801\uff1a\", resp.status_code)\nprint(\"\u8fd4\u56de\u7c7b\u578b\uff1a\", resp.headers.get(\"Content-Type\"))\nprint(\"\u8fd4\u56de\u524d 120 \u4e2a\u5b57\u7b26\uff1a\")\nprint(resp.text&#91;:120])\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>resp.headers.get(\"Content-Type\")<\/code> \u770b\u8fd4\u56de\u7c7b\u578b\u2014\u2014<code>application\/json<\/code> \u8bf4\u660e\u8fd4\u56de JSON\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>URL \u7ed3\u6784\u62c6\u89e3<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https:&#47;&#47;api.open-meteo.com\/v1\/forecast?latitude=39.9042&amp;longitude=116.4074\n\u2502____________________________\u2502 \u2502________\u2502 \u2502_________________________\u2502\n        \u534f\u8bae + \u57df\u540d              \u8def\u5f84       \u67e5\u8be2\u53c2\u6570\uff08\u95ee\u53f7 ? \u4e4b\u540e\uff09\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u8be2\u53c2\u6570 <code>\u952e=\u503c<\/code> \u7528 <code>&amp;<\/code> \u8fde\u63a5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1aGET \u8bf7\u6c42\u91cc\u653e\u5bc6\u7801 \u2192 \u95ee\u9898\uff1a\u53c2\u6570\u5728 URL \u91cc\uff0c\u4f1a\u88ab\u65e5\u5fd7\/\u5386\u53f2\u8bb0\u5f55\u66b4\u9732 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u654f\u611f\u4fe1\u606f\u7528 POST \u653e\u8bf7\u6c42\u4f53\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u53c2\u6570\u503c\u542b\u7279\u6b8a\u5b57\u7b26\uff08<code>\/<\/code>\u3001<code>&amp;<\/code>\uff09 \u2192 \u95ee\u9898\uff1a\u76f4\u63a5\u62fc\u4f1a\u7834\u574f URL \u7ed3\u6784 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528 <code>params<\/code> \u5b57\u5178\u4f20\u53c2\uff0c\u81ea\u52a8\u7f16\u7801\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u628a POST \u5f53 GET \u7528 \u2192 \u95ee\u9898\uff1a\u8bed\u4e49\u6df7\u4e71\uff0c\u670d\u52a1\u5668\u53ef\u80fd\u62d2\u7edd \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u8bfb\u6570\u636e\u7528 GET\uff0c\u4ea7\u751f\u53d8\u5316\u7528 POST\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1aGET \u8981\u6570\u636e\uff0cPOST \u63d0\u6570\u636e\uff1b\u8d44\u6e90\u7528 URL\uff0c\u64cd\u4f5c\u9760\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2 JSON \u2014\u2014 API \u7684\u901a\u7528\u8bed\u8a00<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>JSON<\/strong>\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u957f\u5f97\u548c Python \u7684\u5b57\u5178\/\u5217\u8868\u51e0\u4e4e\u4e00\u6837\uff1a<code>{\"\u952e\": \u503c, ...}<\/code>\u3001<code>[\u5143\u7d20, \u5143\u7d20]<\/code>\u3002\u51e0\u4e4e\u6240\u6709\u73b0\u4ee3 API \u90fd\u7528 JSON \u8fd4\u56de\u6570\u636e\uff0c\u5b66\u4f1a\u89e3\u6790 JSON \u5c31\u7b49\u4e8e\u5b66\u4f1a\u8bfb API \u7684\u300c\u56de\u4fe1\u300d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">JSON \u5b57\u7b26\u4e32\u548c Python \u5bf9\u8c61\u7684\u4e92\u8f6c\uff1a<code>json.loads(\u5b57\u7b26\u4e32)<\/code> \u628a JSON \u8f6c\u6210\u5b57\u5178\/\u5217\u8868\uff08<code>json.dumps<\/code> \u53cd\u5411\uff09\uff1brequests \u66f4\u8d34\u5fc3\u2014\u2014<code>resp.json()<\/code> \u4e00\u6b65\u5230\u4f4d\u81ea\u52a8\u89e3\u6790\uff0c\u5185\u90e8\u5c31\u662f <code>json.loads(resp.text)<\/code>\u3002<strong>JSON \u91cc\u7684\u952e\u662f\u5b57\u7b26\u4e32\uff0c\u503c\u53ef\u4ee5\u662f\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u5e03\u5c14\u3001\u6570\u7ec4\u3001\u5d4c\u5957\u5bf9\u8c61<\/strong>\u3002\u89e3\u6790\u5931\u8d25\u4f1a\u629b <code>json.JSONDecodeError<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u50cf<strong>\u586b\u5199\u56fd\u9645\u5feb\u9012\u5355<\/strong>\uff1a\u5404\u56fd\u586b\u5199\u683c\u5f0f\u4e0d\u540c\uff08JSON \u662f\u300c\u56fd\u9645\u901a\u7528\u683c\u5f0f\u300d\uff09\uff0c\u5feb\u9012\u516c\u53f8\uff08requests\uff09\u5e2e\u4f60\u7ffb\u8bd1\u6210\u4e2d\u6587\uff08Python \u5b57\u5178\uff09\uff0c\u4f60\u76f4\u63a5\u8bfb\u5c31\u884c\uff0c\u4e0d\u7528\u81ea\u5df1\u5b66\u5916\u8bed\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>JSON \u5b57\u7b26\u4e32\u4e0e Python \u5bf9\u8c61\u4e92\u8f6c<\/strong>\uff08\u79bb\u7ebf\u53ef\u8dd1\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\ntext = '{\"name\": \"\u5317\u4eac\", \"weather\": \"\u6674\", \"temp\": 32, \"hours\": &#91;\"8\u65f6\", \"14\u65f6\"]}'\ndata = json.loads(text)              # \u5b57\u7b26\u4e32 -&gt; \u5b57\u5178\nprint(data&#91;\"name\"])                  # \u5317\u4eac\nprint(data&#91;\"temp\"] + 1)              # \u6570\u5b57\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\uff1a33\nprint(data&#91;\"hours\"]&#91;0])              # \u5d4c\u5957\u5217\u8868\u6309\u4e0b\u6807\u53d6\uff1a8\u65f6\n\nback = json.dumps({\"city\": \"\u4e0a\u6d77\"}, ensure_ascii=False)   # \u5b57\u5178 -&gt; \u5b57\u7b26\u4e32\nprint(back)                          # {\"city\": \"\u4e0a\u6d77\"}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5317\u4eac\n33\n8\u65f6\n{\"city\": \"\u4e0a\u6d77\"}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>json.loads(text)<\/code> \u628a JSON \u5b57\u7b26\u4e32\u8f6c\u6210\u5b57\u5178\uff1b<code>data[\"temp\"] + 1<\/code> \u8bf4\u660e\u6570\u5b57\u8f6c\u6210 int \u53ef\u76f4\u63a5\u8fd0\u7b97\uff1b<code>json.dumps(..., ensure_ascii=False)<\/code> \u8ba9\u4e2d\u6587\u4e0d\u53d8\u6210\u8f6c\u4e49\u7801\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>resp.json() \u4e00\u6b65\u89e3\u6790 API \u8fd4\u56de<\/strong>\uff08\u9700\u8981\u8054\u7f51\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\n\nresp = requests.get(\"https:\/\/api.open-meteo.com\/v1\/forecast?latitude=39.9042&amp;longitude=116.4074\",\n                    timeout=10)\ndata = resp.json()                   # \u81ea\u52a8\u628a JSON \u89e3\u6790\u6210 Python \u5b57\u5178\nprint(type(data))                    # &lt;class 'dict'&gt;\nprint(\"\u65f6\u533a\uff1a\", data&#91;\"timezone\"])\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1aJSON \u952e\u540d\u8bb0\u9519 \u2192 \u95ee\u9898\uff1a\u62a5 <code>KeyError<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5148 <code>print(data)<\/code> \u770b\u771f\u5b9e\u7ed3\u6784\u518d\u53d6\u503c\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5fd8\u4e86 <code>ensure_ascii=False<\/code> \u2192 \u95ee\u9898\uff1a\u4e2d\u6587\u88ab\u8f6c\u6210\u53cd\u659c\u6760\u52a0 u \u7684\u8f6c\u4e49 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a<code>json.dumps(data, ensure_ascii=False)<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u8fd4\u56de\u7684\u4e0d\u662f JSON \u5374\u8c03 <code>resp.json()<\/code> \u2192 \u95ee\u9898\uff1a\u629b <code>json.JSONDecodeError<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5148 <code>print(resp.text)<\/code> \u770b\u8fd4\u56de\u4e86\u4ec0\u4e48\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1aJSON \u50cf\u5b57\u5178\uff0cloads \u53d8\u5bf9\u8c61\uff1bdumps \u8f6c\u56de\u53bb\uff0censure_ascii \u4fdd\u4e2d\u6587\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3 GET \u8bf7\u6c42\u4e0e\u67e5\u8be2\u53c2\u6570 \u2014\u2014 \u7528 params \u4f18\u96c5\u4f20\u53c2<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8c03\u7528 API \u7ecf\u5e38\u8981\u5e26\u53c2\u6570\uff08\u7ecf\u7eac\u5ea6\u3001\u57ce\u5e02\u3001\u9875\u6570\uff09\u3002\u624b\u62fc URL \u5bb9\u6613\u9519\u3001\u5bb9\u6613\u6f0f\u8f6c\u4e49\uff1b<code>requests.get(url, params={...})<\/code> \u7528\u5b57\u5178\u4f20\u53c2\uff0crequests \u81ea\u52a8\u5b8c\u6210 URL \u7f16\u7801\uff08\u6bd4\u5982\u628a <code>\/<\/code> \u53d8\u6210 <code>%2F<\/code>\uff09\uff0c\u4ee3\u7801\u53c8\u5e72\u51c0\u53c8\u4e0d\u5bb9\u6613\u9519\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>params<\/code> \u5b57\u5178\u91cc\u6bcf\u4e2a\u952e\u503c\u5bf9\u4f1a\u53d8\u6210 <code>?\u952e=\u503c&amp;\u952e=\u503c<\/code> \u62fc\u5230 URL \u540e\u9762\uff1b\u503c\u91cc\u7684\u7279\u6b8a\u5b57\u7b26\u81ea\u52a8\u7f16\u7801\u3002Open-Meteo \u6838\u5fc3\u53c2\u6570\uff1a<code>latitude<\/code> \/ <code>longitude<\/code>\uff08\u7ecf\u7eac\u5ea6\uff09\u3001<code>daily<\/code>\uff08\u8981\u54ea\u4e9b\u6bcf\u65e5\u6570\u636e\uff0c\u9017\u53f7\u5206\u9694\uff09\u3001<code>timezone<\/code>\uff08\u65f6\u533a\uff0c\u5fc5\u987b\u5199 <code>Asia\/Shanghai<\/code>\uff0c\u5426\u5219\u8fd4\u56de UTC \u65f6\u95f4\uff09\u3001<code>forecast_days<\/code>\uff08\u9884\u6d4b\u5929\u6570\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u50cf<strong>\u4fbf\u5229\u5e97\u70b9\u5355<\/strong>\uff1a<code>params<\/code> \u662f\u70b9\u5355\u7eb8\uff0c\u5199\u4e0a\u300c\u5927\u676f\u3001\u5c11\u51b0\u3001\u52a0\u73cd\u73e0\u300d\uff08\u952e=\u503c\uff09\uff0c\u5e97\u5458\uff08\u670d\u52a1\u5668\uff09\u6309\u5355\u5b50\u505a\u3002\u70b9\u5355\u7eb8\u6bd4\u53e3\u5934\u558a\uff08\u624b\u62fc URL\uff09\u4e0d\u5bb9\u6613\u51fa\u9519\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u7528 params \u4f20\u53c2\u8c03\u7528\u5929\u6c14 API<\/strong>\uff08\u9700\u8981\u8054\u7f51\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\n\nparams = {\n    \"latitude\": 39.9042,\n    \"longitude\": 116.4074,\n    \"daily\": \"temperature_2m_max,temperature_2m_min\",\n    \"timezone\": \"Asia\/Shanghai\",\n    \"forecast_days\": 3,\n}\nresp = requests.get(\"https:\/\/api.open-meteo.com\/v1\/forecast\",\n                    params=params, timeout=10)\nresp.raise_for_status()          # \u72b6\u6001\u7801\u975e 2xx \u76f4\u63a5\u629b\u5f02\u5e38\ndata = resp.json()&#91;\"daily\"]      # \u89e3\u6790\u5e76\u76f4\u63a5\u53d6 daily \u90e8\u5206\n\nfor date, tmax, tmin in zip(data&#91;\"time\"],\n                            data&#91;\"temperature_2m_max\"],\n                            data&#91;\"temperature_2m_min\"]):\n    print(f\"{date}\uff1a\u6700\u9ad8 {tmax}\u2103 \/ \u6700\u4f4e {tmin}\u2103\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>params<\/code> \u5b57\u5178\u5217\u51fa\u53c2\u6570\uff0c<code>requests.get(url, params=params)<\/code> \u81ea\u52a8\u62fc URL \u5e76\u7f16\u7801\uff1b<code>resp.raise_for_status()<\/code> \u72b6\u6001\u7801\u4e0d\u662f 2xx \u5c31\u629b\u5f02\u5e38\uff1b<code>resp.json()[\"daily\"]<\/code> \u89e3\u6790\u540e\u76f4\u63a5\u53d6 <code>daily<\/code> \u952e\uff1b<code>zip(...)<\/code> \u628a\u65e5\u671f\u3001\u6700\u9ad8\u6e29\u3001\u6700\u4f4e\u6e29\u4e09\u4e2a\u5217\u8868\u6309\u4f4d\u7f6e\u6253\u5305\u904d\u5386\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5fd8\u5199 <code>timezone<\/code> \u53c2\u6570 \u2192 \u95ee\u9898\uff1a\u8fd4\u56de UTC \u65f6\u95f4\uff0c\u65e5\u671f\u53ef\u80fd\u300c\u5dee\u4e00\u5929\u300d \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u4f20 <code>\"timezone\": \"Asia\/Shanghai\"<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u53c2\u6570\u540d\u62fc\u9519\uff08\u5982 <code>lat<\/code> \u4e0d\u662f <code>latitude<\/code>\uff09 \u2192 \u95ee\u9898\uff1a\u53c2\u6570\u4e0d\u751f\u6548\uff0c\u8fd4\u56de\u9ed8\u8ba4\u4f4d\u7f6e\u6570\u636e \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5bf9\u7167 API \u6587\u6863\u68c0\u67e5\u53c2\u6570\u540d\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>daily<\/code> \u53c2\u6570\u4e0d\u6307\u5b9a \u2192 \u95ee\u9898\uff1a\u53ea\u8fd4\u56de <code>current_weather<\/code>\uff0c\u6ca1\u6709\u6bcf\u65e5\u6570\u636e \u2192 \u6b63\u786e\u5199\u6cd5\uff1a<code>\"daily\": \"temperature_2m_max,temperature_2m_min\"<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1aparams \u4f20\u53c2\u6700\u4f18\u96c5\uff0c\u7f16\u7801\u8f6c\u4e49\u5168\u81ea\u52a8\uff1btimezone \u8981\u5199\u5bf9\uff0c\u4e0d\u7136\u65e5\u671f\u5dee\u4e00\u5929\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4 POST \u8bf7\u6c42 \u2014\u2014 \u7528 data \u548c json \u63d0\u4ea4\u6570\u636e<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">GET \u53ea\u80fd\u62ff\u6570\u636e\uff0c\u60f3\u300c\u63d0\u4ea4\u300d\uff08\u767b\u5f55\u3001\u4e0b\u5355\u3001\u53d1\u6d88\u606f\uff09\u5fc5\u987b\u7528 POST\u3002requests \u63d0\u4f9b\u4e24\u79cd\u63d0\u4ea4\u683c\u5f0f\uff1a<code>data={...}<\/code> \u6309\u8868\u5355\u683c\u5f0f\uff08<code>application\/x-www-form-urlencoded<\/code>\uff09\u63d0\u4ea4\uff0c<code>json={...}<\/code> \u6309 JSON \u683c\u5f0f\u63d0\u4ea4\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>requests.post(url, data={\"city\": \"\u5317\u4eac\"})<\/code> \u628a\u5b57\u5178\u7f16\u7801\u6210\u8868\u5355\u683c\u5f0f\u653e\u8fdb\u8bf7\u6c42\u4f53\uff1b<code>requests.post(url, json={\"city\": \"\u5317\u4eac\"})<\/code> \u628a\u5b57\u5178\u8f6c\u6210 JSON \u5b57\u7b26\u4e32\u5e76\u81ea\u52a8\u8bbe\u7f6e <code>Content-Type: application\/json<\/code>\u3002<strong>\u533a\u522b\u5c31\u5728\u8bf7\u6c42\u4f53\u683c\u5f0f\u548c Content-Type \u5934<\/strong>\u2014\u2014\u670d\u52a1\u5668\u6309\u5934\u91cc\u58f0\u660e\u7684\u683c\u5f0f\u89e3\u6790\u3002httpbin.org \u662f\u514d\u8d39\u7684\u56de\u663e\u6d4b\u8bd5\u63a5\u53e3\uff0c\u4f60 POST \u4ec0\u4e48\u5b83\u539f\u6837\u8fd4\u56de\u4ec0\u4e48\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u50cf<strong>\u5bc4\u5feb\u9012\u586b\u5355<\/strong>\uff1adata \u662f\u300c\u666e\u901a\u8868\u5355\u300d\uff08\u7eb8\u8d28\u5355\uff09\uff0cjson \u662f\u300c\u7535\u5b50\u5355\u300d\uff08\u626b\u7801\u5f55\u5165\uff09\u3002\u4e24\u79cd\u90fd\u80fd\u5bc4\u5230\uff08\u670d\u52a1\u5668\u90fd\u80fd\u6536\u5230\uff09\uff0c\u4f46\u683c\u5f0f\u4e0d\u540c\uff0c\u522b\u6df7\u7740\u5199\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>POST \u7684\u4e24\u79cd\u63d0\u4ea4\u683c\u5f0f<\/strong>\uff08\u9700\u8981\u8054\u7f51\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\n\n# \u683c\u5f0f\u4e00\uff1adata \u8868\u5355\nr1 = requests.post(\"https:\/\/httpbin.org\/post\",\n                   data={\"city\": \"\u5317\u4eac\", \"days\": 3}, timeout=10)\nd1 = r1.json()\nprint(\"\u8868\u5355\u6536\u5230\u7684\u6570\u636e\uff1a\", d1&#91;\"form\"])          # {'city': '\u5317\u4eac', 'days': '3'}\nprint(\"Content-Type\uff1a\", d1&#91;\"headers\"]&#91;\"Content-Type\"])  # \u8868\u5355\u683c\u5f0f\n\n# \u683c\u5f0f\u4e8c\uff1ajson\nr2 = requests.post(\"https:\/\/httpbin.org\/post\",\n                   json={\"city\": \"\u5317\u4eac\", \"days\": 3}, timeout=10)\nd2 = r2.json()\nprint(\"JSON \u6536\u5230\u7684\u6570\u636e\uff1a\", d2&#91;\"json\"])         # {'city': '\u5317\u4eac', 'days': 3}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e24\u6b21 POST \u90fd\u53d1\u5230 <code>https:\/\/httpbin.org\/post<\/code>\uff08\u56de\u663e\u63a5\u53e3\uff09\uff1b<code>data=<\/code> \u63d0\u4ea4\u65f6\u6570\u636e\u653e\u5728 <code>form<\/code> \u5b57\u6bb5\u8fd4\u56de\uff0c\u6ce8\u610f <code>days<\/code> \u53d8\u6210\u5b57\u7b26\u4e32 <code>'3'<\/code>\uff1b<code>json=<\/code> \u63d0\u4ea4\u65f6\u6570\u636e\u653e\u5728 <code>json<\/code> \u5b57\u6bb5\u8fd4\u56de\uff0c<code>days<\/code> \u4fdd\u6301\u6570\u5b57 <code>3<\/code>\u2014\u2014<strong>\u8fd9\u662f\u4e24\u79cd\u683c\u5f0f\u6700\u76f4\u89c2\u7684\u533a\u522b<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1aPOST \u4e0d\u52a0 <code>timeout<\/code> \u2192 \u95ee\u9898\uff1a\u670d\u52a1\u5668\u4e0d\u54cd\u5e94\u65f6\u65e0\u9650\u5361\u4f4f \u2192 \u6b63\u786e\u5199\u6cd5\uff1a<code>requests.post(url, data=..., timeout=10)<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u60f3\u53d1 JSON \u5374\u7528 <code>data=<\/code> \u2192 \u95ee\u9898\uff1a\u670d\u52a1\u5668\u6309\u8868\u5355\u89e3\u6790\uff0c\u5d4c\u5957\u6570\u636e\u4f1a\u4e22 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u53d1 JSON \u7528 <code>json=<\/code> \u53c2\u6570\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u628a GET \u53c2\u6570\u62fc\u5728 POST \u7684 URL \u91cc \u2192 \u95ee\u9898\uff1a\u6570\u636e\u6ca1\u8fdb\u8bf7\u6c42\u4f53\uff0c\u670d\u52a1\u5668\u8bfb\u4e0d\u5230 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u6570\u636e\u653e <code>data=<\/code> \u6216 <code>json=<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1aPOST \u63d0\u4ea4\u4e24\u79cd\u59ff\uff1adata \u8868\u5355 json \u7535\u5b50\u5355\uff1b\u6570\u636e\u8981\u8fdb\u8bf7\u6c42\u4f53\uff0ctimeout \u522b\u5fd8\u52a0\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5 \u9519\u8bef\u5904\u7406 \u2014\u2014 \u4e09\u5c42\u4fdd\u9669\uff0c\u8c03\u7528\u4e0d\u5d29<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">API \u8c03\u7528\u5931\u8d25\u5f88\u5e38\u89c1\uff1a\u65ad\u7f51\u3001\u8d85\u65f6\u3001\u53c2\u6570\u9519\uff08400\uff09\u3001\u63a5\u53e3\u4e0d\u5b58\u5728\uff08404\uff09\u3001\u670d\u52a1\u5668\u6545\u969c\uff08500\uff09\u3001\u8fd4\u56de\u4e0d\u662f JSON\u3002\u4e0d\u52a0\u4fdd\u62a4\u7684\u8c03\u7528\uff0c\u4e00\u4e2a\u5f02\u5e38\u5c31\u5d29\u6e83\u3002\u5b66\u4f1a <code>timeout<\/code> + <code>raise_for_status<\/code> + <code>try\/except<\/code> \u4e09\u5c42\u4fdd\u9669\uff0c\u662f\u8c03\u7528\u4efb\u4f55 API \u7684\u804c\u4e1a\u4e60\u60ef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e09\u5c42\u5206\u5de5\uff1a**<code>timeout=10<\/code> \u7ba1\u300c\u7b49\u591a\u4e45\u300d<strong>\u2014\u201410 \u79d2\u6ca1\u54cd\u5e94\u5c31\u653e\u5f03\uff1b<\/strong><code>raise_for_status()<\/code> \u7ba1\u300cHTTP \u5c42\u300d<strong>\u2014\u2014\u72b6\u6001\u7801\u975e 2xx \u629b <code>HTTPError<\/code>\uff08400\u3001404\u3001500 \u90fd\u7b97\uff09\uff1b<\/strong><code>try\/except<\/code> \u7ba1\u300c\u7f51\u7edc\u4e0e\u89e3\u6790\u5c42\u300d**\u2014\u2014<code>ConnectionError<\/code> \u8fde\u4e0d\u4e0a\u3001<code>Timeout<\/code> \u8d85\u65f6\u3001<code>ValueError<\/code> JSON \u89e3\u6790\u5931\u8d25\uff08<code>JSONDecodeError<\/code> \u662f <code>ValueError<\/code> \u7684\u5b50\u7c7b\uff09\u3002\u5f02\u5e38\u4ece\u5185\u5230\u5916\uff0c\u4e00\u7f51\u6253\u5c3d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u50cf<strong>\u5916\u5356\u4e09\u5c42\u4fdd\u969c<\/strong>\uff1a<code>timeout<\/code> \u662f\u300c30 \u5206\u949f\u4e0d\u9001\u5230\u6211\u5c31\u53d6\u6d88\u300d\uff1b<code>raise_for_status<\/code> \u662f\u300c\u5546\u5bb6\u8bf4\u6ca1\u8fd9\u4e2a\u83dc\u300d\uff08\u72b6\u6001\u660e\u786e\u544a\u77e5\uff09\uff1b<code>try\/except<\/code> \u662f\u300c\u9a91\u624b\u8ff7\u8def\u4e86\u4e5f\u5148\u5b89\u6170\u4f60\u522b\u5d29\u6e83\u300d\uff08\u51fa\u4e86\u5c94\u5b50\u7a0b\u5e8f\u7167\u6837\u6d3b\u7740\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u4e09\u5c42\u4fdd\u62a4\u7684\u5929\u6c14\u67e5\u8be2\u51fd\u6570<\/strong>\uff08\u9700\u8981\u8054\u7f51\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\n\ndef fetch_weather(latitude, longitude):\n    params = {\n        \"latitude\": latitude,\n        \"longitude\": longitude,\n        \"daily\": \"temperature_2m_max,temperature_2m_min\",\n        \"timezone\": \"Asia\/Shanghai\",\n        \"forecast_days\": 3,\n    }\n    try:\n        resp = requests.get(\"https:\/\/api.open-meteo.com\/v1\/forecast\",\n                            params=params, timeout=10)\n        resp.raise_for_status()\n        return resp.json()\n    except requests.exceptions.ConnectionError:\n        print(\"\u7f51\u7edc\u8fde\u63a5\u5931\u8d25\uff1a\u8bf7\u68c0\u67e5\u7f51\u7edc\")\n    except requests.exceptions.Timeout:\n        print(\"\u8bf7\u6c42\u8d85\u65f6\uff1a\u670d\u52a1\u5668\u54cd\u5e94\u592a\u6162\")\n    except requests.exceptions.HTTPError as e:\n        print(f\"HTTP \u9519\u8bef\uff1a{e}\")      # \u5982 400 \u53c2\u6570\u9519\u8bef\u3001404 \u63a5\u53e3\u4e0d\u5b58\u5728\n    except ValueError:\n        print(\"\u8fd4\u56de\u5185\u5bb9\u4e0d\u662f\u6709\u6548\u7684 JSON\")\n    return None\n\ndata = fetch_weather(39.9042, 116.4074)\nif data:\n    print(\"\u8c03\u7528\u6210\u529f\uff0c\u6700\u9ad8\u6e29\uff1a\", data&#91;\"daily\"]&#91;\"temperature_2m_max\"])\nelse:\n    print(\"\u8c03\u7528\u5931\u8d25\uff0c\u672a\u62ff\u5230\u6570\u636e\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u5c01\u88c5\u8bf7\u6c42\uff1b<code>try<\/code> \u91cc\u4e09\u6b65\u8d70\uff08\u8bf7\u6c42 \u2192 \u68c0\u67e5\u72b6\u6001\u7801 \u2192 \u89e3\u6790 JSON\uff09\uff1b\u56db\u4e2a <code>except<\/code> \u5206\u522b\u63a5\u4f4f\u8fde\u63a5\u5931\u8d25\u3001\u8d85\u65f6\u3001HTTP \u9519\u8bef\u3001JSON \u89e3\u6790\u5931\u8d25\u2014\u2014<strong>\u4efb\u4f55\u73af\u8282\u51fa\u9519\u90fd\u4e0d\u5d29\u6e83\uff0c\u8fd4\u56de None \u7531\u8c03\u7528\u65b9\u5224\u65ad<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u53ea <code>try\/except<\/code> \u4e0d\u5199 <code>raise_for_status()<\/code> \u2192 \u95ee\u9898\uff1a404 \u9875\u9762\u4e5f\u80fd\u300c\u6210\u529f\u300d\u8fd4\u56de\uff0c\u89e3\u6790\u51fa\u9519 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u72b6\u6001\u7801\u68c0\u67e5\u4e0e\u5f02\u5e38\u5904\u7406\u4e24\u5c42\u90fd\u8981\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>except Exception<\/code> \u4e00\u628a\u6293 \u2192 \u95ee\u9898\uff1a\u8fde\u4ee3\u7801 bug \u90fd\u541e\u6389\uff0c\u96be\u6392\u67e5 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u6309\u5f02\u5e38\u7c7b\u578b\u5206\u522b\u63a5\u4f4f\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u4e0d\u52a0 <code>timeout<\/code> \u2192 \u95ee\u9898\uff1a\u670d\u52a1\u5668\u4e0d\u54cd\u5e94\u65f6 try \u91cc\u65e0\u9650\u7b49 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a<code>timeout=10<\/code> \u5fc5\u987b\u5199\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1atimeout \u7ba1\u7b49\u5f85\uff0craise \u7ba1\u72b6\u6001\uff0cexcept \u7ba1\u515c\u5e95\uff1b\u4e09\u5c42\u4fdd\u9669\u9f50\uff0c\u8c03\u7528\u6c38\u4e0d\u5d29\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6 \u52a8\u624b\u5b9e\u8df5\uff1a\u57ce\u5e02\u5929\u6c14\u67e5\u8be2\u5de5\u5177<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u505a\u4e00\u4e2a\u300c\u5929\u6c14\u67e5\u8be2\u5de5\u5177\u300d\uff1a\u5185\u7f6e\u51e0\u4e2a\u57ce\u5e02\u548c\u7ecf\u7eac\u5ea6\uff0c\u8f93\u5165\u57ce\u5e02\u540d\uff0c\u8c03\u7528 Open-Meteo \u8fd4\u56de\u672a\u6765 3 \u5929\u5929\u6c14\u5e76\u6253\u5370\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># weather_api.py \u2014\u2014 \u57ce\u5e02\u5929\u6c14\u67e5\u8be2\u5de5\u5177\nimport requests\n\n# \u57ce\u5e02\u540d -&gt; (\u7eac\u5ea6, \u7ecf\u5ea6)\nCITIES = {\n    \"\u5317\u4eac\": (39.9042, 116.4074),\n    \"\u4e0a\u6d77\": (31.2304, 121.4737),\n    \"\u5e7f\u5dde\": (23.1291, 113.2644),\n    \"\u6df1\u5733\": (22.5431, 114.0579),\n    \"\u6210\u90fd\": (30.5728, 104.0668),\n}\n\ndef query_weather(city):\n    if city not in CITIES:\n        print(f\"\u6ca1\u6709\u300c{city}\u300d\u7684\u5750\u6807\uff0c\u53ef\u9009\uff1a{'\u3001'.join(CITIES)}\")\n        return\n    lat, lon = CITIES&#91;city]\n    params = {\n        \"latitude\": lat,\n        \"longitude\": lon,\n        \"daily\": \"temperature_2m_max,temperature_2m_min,precipitation_sum\",\n        \"timezone\": \"Asia\/Shanghai\",\n        \"forecast_days\": 3,\n    }\n    try:\n        resp = requests.get(\"https:\/\/api.open-meteo.com\/v1\/forecast\",\n                            params=params, timeout=10)\n        resp.raise_for_status()\n        data = resp.json()&#91;\"daily\"]\n        print(f\"\\n{city} \u672a\u6765 3 \u5929\u5929\u6c14\uff1a\")\n        for date, tmax, tmin, rain in zip(\n                data&#91;\"time\"], data&#91;\"temperature_2m_max\"],\n                data&#91;\"temperature_2m_min\"], data&#91;\"precipitation_sum\"]):\n            print(f\"  {date}  \u6700\u9ad8 {tmax}\u2103 \/ \u6700\u4f4e {tmin}\u2103  \u964d\u6c34 {rain}mm\")\n    except requests.exceptions.RequestException as e:\n        print(f\"\u8bf7\u6c42\u5931\u8d25\uff1a{e}\")\n    except (KeyError, ValueError):\n        print(\"\u8fd4\u56de\u6570\u636e\u683c\u5f0f\u5f02\u5e38\")\n\nif __name__ == \"__main__\":\n    city = input(\"\u8f93\u5165\u57ce\u5e02\u540d\uff08\u5317\u4eac\/\u4e0a\u6d77\/\u5e7f\u5dde\/\u6df1\u5733\/\u6210\u90fd\uff09\uff1a\").strip()\n    query_weather(city)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u6548\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8f93\u5165\u57ce\u5e02\u540d\uff08\u5317\u4eac\/\u4e0a\u6d77\/\u5e7f\u5dde\/\u6df1\u5733\/\u6210\u90fd\uff09\uff1a\u4e0a\u6d77\n\n\u4e0a\u6d77 \u672a\u6765 3 \u5929\u5929\u6c14\uff1a\n  2026-08-03  \u6700\u9ad8 33.2\u2103 \/ \u6700\u4f4e 26.8\u2103  \u964d\u6c34 0.0mm\n  2026-08-04  \u6700\u9ad8 31.5\u2103 \/ \u6700\u4f4e 25.9\u2103  \u964d\u6c34 5.2mm\n  2026-08-05  \u6700\u9ad8 34.0\u2103 \/ \u6700\u4f4e 26.2\u2103  \u964d\u6c34 0.0mm\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u5165\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u57ce\u5e02\u540d\uff0c\u7a0b\u5e8f\u4e5f\u80fd\u4f18\u96c5\u63d0\u793a\uff0c\u4e0d\u4f1a\u5d29\u6e83\u3002\u8fd9\u5c31\u662f\u300c\u5de5\u5177\u300d\u8be5\u6709\u7684\u6837\u5b50\uff1a<strong>\u80fd\u7528\u7684\u529f\u80fd + \u5408\u7406\u7684\u5bb9\u9519<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5347\u7ea7\u6311\u6218<\/strong>\uff1a\u628a\u67e5\u8be2\u7ed3\u679c\u4fdd\u5b58\u6210 JSON \u6587\u4ef6\uff08\u56de\u5fc6 Day 12 \u7684 <code>json.dump<\/code>\uff09\uff0c\u5e76\u987a\u624b\u628a <code>forecast_days<\/code> \u6539\u6210 7\uff0c\u770b\u770b\u4e00\u5468\u7684\u6570\u636e\u957f\u4ec0\u4e48\u6837\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4eca\u65e5\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">HTTP \u4e0e REST\uff1aGET \u8981\u6570\u636e\u3001POST \u63d0\u6570\u636e\uff1bURL = \u534f\u8bae + \u57df\u540d + \u8def\u5f84 + \u67e5\u8be2\u53c2\u6570\uff1bGET \u53c2\u6570\u522b\u653e\u5bc6\u7801\nJSON\uff1a<code>json.loads<\/code> \/ <code>json.dumps(ensure_ascii=False)<\/code>\u3001<code>resp.json()<\/code> \u4e00\u6b65\u89e3\u6790\uff0c\u5d4c\u5957\u7ed3\u6784\u6309\u5c42\u7ea7\u53d6\u503c\nGET \u4f20\u53c2\uff1a<code>params<\/code> \u5b57\u5178\u81ea\u52a8\u7f16\u7801\uff0c<code>timezone=Asia\/Shanghai<\/code> \u522b\u5fd8\u5199\nPOST \u63d0\u4ea4\uff1a<code>data=<\/code> \u8868\u5355 vs <code>json=<\/code> JSON\uff0chttpbin \u56de\u663e\u63a5\u53e3\u5b9e\u6d4b\u4e24\u79cd\u683c\u5f0f\u7684\u533a\u522b\n\u4e09\u5c42\u4fdd\u62a4\uff1a<code>timeout<\/code> + <code>raise_for_status()<\/code> + <code>try\/except<\/code>\uff08ConnectionError \/ Timeout \/ HTTPError \/ ValueError\uff09\n\u57ce\u5e02\u5929\u6c14\u67e5\u8be2\u5de5\u5177\u80fd\u67e5\u3001\u80fd\u5bb9\u9519\uff0cAPI \u8c03\u7528\u5168\u6d41\u7a0b\u95ed\u73af<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u62a5\u9519<\/th><th>\u539f\u56e0<\/th><th>\u4fee\u590d<\/th><\/tr><\/thead><tbody><tr><td><code>requests.exceptions.ConnectionError<\/code><\/td><td>\u8fde\u4e0d\u4e0a API \u670d\u52a1\u5668\uff08\u65ad\u7f51\u3001\u57df\u540d\u62fc\u9519\uff09<\/td><td>\u68c0\u67e5\u7f51\u7edc\uff1b\u786e\u8ba4 URL \u5b8c\u6574\u4e14\u80fd\u76f4\u63a5\u5728\u6d4f\u89c8\u5668\u6253\u5f00<\/td><\/tr><tr><td><code>requests.exceptions.Timeout<\/code><\/td><td>\u670d\u52a1\u5668\u54cd\u5e94\u592a\u6162\uff0c\u8d85\u8fc7\u7b49\u5f85\u65f6\u95f4<\/td><td>\u52a0 <code>timeout=10<\/code> \u5e76\u8c03\u5927\uff0c\u6216\u7a0d\u540e\u91cd\u8bd5<\/td><\/tr><tr><td><code>requests.exceptions.HTTPError: 400 Client Error<\/code><\/td><td>\u53c2\u6570\u5199\u9519\u6216\u7f3a\u5c11\u5fc5\u586b\u53c2\u6570<\/td><td>\u5bf9\u7167 API \u6587\u6863\u68c0\u67e5\u53c2\u6570\u540d\u3001\u683c\u5f0f\u3001\u5355\u4f4d<\/td><\/tr><tr><td><code>KeyError: 'temperature_2m_max'<\/code><\/td><td>\u8fd4\u56de\u7ed3\u6784\u91cc\u6ca1\u6709\u8fd9\u4e2a\u952e\uff08\u53c2\u6570\u6ca1\u751f\u6548\u6216\u63a5\u53e3\u6539\u7248\uff09<\/td><td>\u5148 <code>print(data)<\/code> \u770b\u5b9e\u9645\u8fd4\u56de\u7ed3\u6784\uff0c\u518d\u8c03\u6574\u53d6\u503c\u8def\u5f84<\/td><\/tr><tr><td><code>json.decoder.JSONDecodeError<\/code>\uff08\u6216 <code>ValueError: Expecting value<\/code>\uff09<\/td><td>\u8fd4\u56de\u5185\u5bb9\u4e0d\u662f JSON\uff08\u6bd4\u5982 404 \u9875\u9762\u3001\u7a7a\u54cd\u5e94\uff09<\/td><td>\u5148\u6253\u5370 <code>resp.text<\/code> \u770b\u8fd4\u56de\u4e86\u4ec0\u4e48\uff1b<code>raise_for_status()<\/code> \u524d\u7f6e\u62e6\u622a\u9519\u8bef\u72b6\u6001\u7801<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>1 HTTP \u4e0e REST \u2014\u2014 \u7a0b\u5e8f\u4e4b\u95f4\u7684\u300c\u666e\u901a\u8bdd\u300d \u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f HTTP\uff08\u8d85\u6587\u672c\u4f20\u8f93\u534f\u8bae\uff09\u662f\u7a0b\u5e8f\u4e4b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[16,9],"tags":[17],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-python","category-code-dev","tag-python-day"],"_links":{"self":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/comments?post=121"}],"version-history":[{"count":2,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/121\/revisions"}],"predecessor-version":[{"id":156,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/121\/revisions\/156"}],"wp:attachment":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/media?parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/categories?post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/tags?post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}