{"id":1131472,"date":"2025-01-08T20:48:50","date_gmt":"2025-01-08T12:48:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1131472.html"},"modified":"2025-01-08T20:48:53","modified_gmt":"2025-01-08T12:48:53","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%b0%86%e7%bd%91%e9%a1%b5json%e6%8f%90%e5%8f%96%e5%87%ba%e6%9d%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1131472.html","title":{"rendered":"\u5982\u4f55\u7528python\u5c06\u7f51\u9875json\u63d0\u53d6\u51fa\u6765"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101233\/97eb72b6-3ba4-4575-ba45-2d6eabd7cf6d.webp\" alt=\"\u5982\u4f55\u7528python\u5c06\u7f51\u9875json\u63d0\u53d6\u51fa\u6765\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u5c06\u7f51\u9875JSON\u63d0\u53d6\u51fa\u6765<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u7528Python\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6JSON\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>requests\u5e93\u3001BeautifulSoup\u5e93\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u7b49\u65b9\u6cd5\u3002<strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0crequests\u5e93\u5e38\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff0cBeautifulSoup\u7528\u4e8e\u89e3\u6790HTML\u5185\u5bb9\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7528\u4e8e\u5339\u914d\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u6a21\u5f0f\u3002<\/strong> \u5176\u4e2d\uff0crequests\u5e93\u662f\u6700\u4e3a\u57fa\u7840\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u63a5\u4e0b\u6765\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528requests\u5e93\u8fdb\u884cHTTP\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>requests\u5e93\u662fPython\u4e2d\u6700\u5e38\u7528\u7684HTTP\u5e93\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u5e76\u83b7\u53d6\u670d\u52a1\u5668\u7684\u54cd\u5e94\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5requests\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86requests\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u53d1\u9001HTTP\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u5e76\u83b7\u53d6\u54cd\u5e94\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &quot;https:\/\/api.example.com\/data&quot;<\/p>\n<p>response = requests.get(url)<\/p>\n<h2><strong>\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if response.status_code == 200:<\/p>\n<p>    json_data = response.json()<\/p>\n<p>    print(json_data)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to retrieve data&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>requests.get()<\/code>\u65b9\u6cd5\u53d1\u9001\u4e00\u4e2aGET\u8bf7\u6c42\uff0c\u5e76\u901a\u8fc7<code>response.json()<\/code>\u65b9\u6cd5\u5c06\u54cd\u5e94\u5185\u5bb9\u89e3\u6790\u4e3aJSON\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0cJSON\u6570\u636e\u5d4c\u5165\u5728\u7f51\u9875\u7684HTML\u5185\u5bb9\u4e2d\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u5e93\u6765\u89e3\u6790HTML\uff0c\u5e76\u63d0\u53d6\u51fa\u5d4c\u5165\u7684JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5BeautifulSoup\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86BeautifulSoup\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u89e3\u6790HTML\u5e76\u63d0\u53d6JSON\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u5e76\u63d0\u53d6JSON\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import json<\/p>\n<p>url = &quot;https:\/\/www.example.com&quot;<\/p>\n<p>response = requests.get(url)<\/p>\n<h2><strong>\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if response.status_code == 200:<\/p>\n<p>    soup = BeautifulSoup(response.text, &#39;html.parser&#39;)<\/p>\n<p>    script_tag = soup.find(&#39;script&#39;, type=&#39;application\/json&#39;)<\/p>\n<p>    if script_tag:<\/p>\n<p>        json_data = json.loads(script_tag.string)<\/p>\n<p>        print(json_data)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;No JSON data found&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Failed to retrieve data&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\uff0c\u5e76\u627e\u5230\u7b2c\u4e00\u4e2a<code>&lt;script&gt;<\/code>\u6807\u7b7e\uff0c\u5176<code>type<\/code>\u5c5e\u6027\u4e3a<code>application\/json<\/code>\u3002\u7136\u540e\uff0c\u5c06\u8be5\u6807\u7b7e\u7684\u5185\u5bb9\u89e3\u6790\u4e3aJSON\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914dJSON\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0cJSON\u6570\u636e\u53ef\u80fd\u5d4c\u5165\u5728HTML\u7684\u67d0\u4e2a\u811a\u672c\u6807\u7b7e\u6216\u5176\u4ed6\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u548c\u63d0\u53d6\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914dJSON\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u548c\u63d0\u53d6JSON\u5b57\u7b26\u4e32\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>import re<\/p>\n<p>import json<\/p>\n<p>url = &quot;https:\/\/www.example.com&quot;<\/p>\n<p>response = requests.get(url)<\/p>\n<h2><strong>\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if response.status_code == 200:<\/p>\n<p>    pattern = re.compile(r&#39;window\\.__INITIAL_STATE__\\s*=\\s*(\\{.*?\\});&#39;)<\/p>\n<p>    match = pattern.search(response.text)<\/p>\n<p>    if match:<\/p>\n<p>        json_data = json.loads(match.group(1))<\/p>\n<p>        print(json_data)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;No JSON data found&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Failed to retrieve data&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u5339\u914d\u5305\u542bJSON\u6570\u636e\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528<code>json.loads()<\/code>\u65b9\u6cd5\u5c06\u5176\u89e3\u6790\u4e3aJSON\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u590d\u6742\u7684JSON\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u63d0\u53d6\u5230\u7684JSON\u6570\u636e\u53ef\u80fd\u975e\u5e38\u590d\u6742\uff0c\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5904\u7406\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u904d\u5386JSON\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u904d\u5386JSON\u6570\u636e\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u6765\u904d\u5386\u590d\u6742\u7684JSON\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def traverse_json(data, indent=0):<\/p>\n<p>    if isinstance(data, dict):<\/p>\n<p>        for key, value in data.items():<\/p>\n<p>            print(&#39; &#39; * indent + str(key) + &#39;:&#39;)<\/p>\n<p>            traverse_json(value, indent + 2)<\/p>\n<p>    elif isinstance(data, list):<\/p>\n<p>        for item in data:<\/p>\n<p>            traverse_json(item, indent + 2)<\/p>\n<p>    else:<\/p>\n<p>        print(&#39; &#39; * indent + str(data))<\/p>\n<h2><strong>\u793a\u4f8bJSON\u6570\u636e<\/strong><\/h2>\n<p>json_data = {<\/p>\n<p>    &quot;name&quot;: &quot;John&quot;,<\/p>\n<p>    &quot;age&quot;: 30,<\/p>\n<p>    &quot;address&quot;: {<\/p>\n<p>        &quot;city&quot;: &quot;New York&quot;,<\/p>\n<p>        &quot;zipcode&quot;: &quot;10001&quot;<\/p>\n<p>    },<\/p>\n<p>    &quot;phoneNumbers&quot;: [<\/p>\n<p>        {&quot;type&quot;: &quot;home&quot;, &quot;number&quot;: &quot;212 555-1234&quot;},<\/p>\n<p>        {&quot;type&quot;: &quot;work&quot;, &quot;number&quot;: &quot;646 555-4567&quot;}<\/p>\n<p>    ]<\/p>\n<p>}<\/p>\n<h2><strong>\u904d\u5386JSON\u6570\u636e<\/strong><\/h2>\n<p>traverse_json(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9012\u5f52\u51fd\u6570<code>traverse_json()<\/code>\uff0c\u53ef\u4ee5\u904d\u5386\u5e76\u6253\u5370JSON\u6570\u636e\u7684\u6bcf\u4e00\u4e2a\u5b57\u6bb5\u3002<\/p>\n<\/p>\n<p><h4>2. \u63d0\u53d6\u7279\u5b9a\u5b57\u6bb5<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ea\u9700\u8981\u63d0\u53d6JSON\u6570\u636e\u4e2d\u7684\u7279\u5b9a\u5b57\u6bb5\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5b57\u5178\u64cd\u4f5c\u6765\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8bJSON\u6570\u636e<\/p>\n<p>json_data = {<\/p>\n<p>    &quot;name&quot;: &quot;John&quot;,<\/p>\n<p>    &quot;age&quot;: 30,<\/p>\n<p>    &quot;address&quot;: {<\/p>\n<p>        &quot;city&quot;: &quot;New York&quot;,<\/p>\n<p>        &quot;zipcode&quot;: &quot;10001&quot;<\/p>\n<p>    },<\/p>\n<p>    &quot;phoneNumbers&quot;: [<\/p>\n<p>        {&quot;type&quot;: &quot;home&quot;, &quot;number&quot;: &quot;212 555-1234&quot;},<\/p>\n<p>        {&quot;type&quot;: &quot;work&quot;, &quot;number&quot;: &quot;646 555-4567&quot;}<\/p>\n<p>    ]<\/p>\n<p>}<\/p>\n<h2><strong>\u63d0\u53d6\u7279\u5b9a\u5b57\u6bb5<\/strong><\/h2>\n<p>name = json_data.get(&quot;name&quot;)<\/p>\n<p>city = json_data[&quot;address&quot;].get(&quot;city&quot;)<\/p>\n<p>home_phone = next((phone[&quot;number&quot;] for phone in json_data[&quot;phoneNumbers&quot;] if phone[&quot;type&quot;] == &quot;home&quot;), None)<\/p>\n<p>print(f&quot;Name: {name}&quot;)<\/p>\n<p>print(f&quot;City: {city}&quot;)<\/p>\n<p>print(f&quot;Home Phone: {home_phone}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u7684<code>get()<\/code>\u65b9\u6cd5\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u63d0\u53d6\u7279\u5b9a\u7684\u5b57\u6bb5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5927\u89c4\u6a21JSON\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cJSON\u6570\u636e\u53ef\u80fd\u975e\u5e38\u5e9e\u5927\uff0c\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u9700\u8981\u8003\u8651\u6027\u80fd\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u751f\u6210\u5668\u5904\u7406\u5927\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u793a\u4f8b\u5927\u89c4\u6a21JSON\u6570\u636e<\/strong><\/h2>\n<p>json_data = &#39;[{&quot;id&quot;: 1, &quot;name&quot;: &quot;John&quot;}, {&quot;id&quot;: 2, &quot;name&quot;: &quot;Jane&quot;}, {&quot;id&quot;: 3, &quot;name&quot;: &quot;Doe&quot;}]&#39;<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u5904\u7406\u5927\u6570\u636e<\/strong><\/h2>\n<p>def parse_large_json(data):<\/p>\n<p>    for item in json.loads(data):<\/p>\n<p>        yield item<\/p>\n<p>for item in parse_large_json(json_data):<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570<code>parse_large_json()<\/code>\uff0c\u53ef\u4ee5\u9010\u4e2a\u5904\u7406\u5927\u89c4\u6a21\u7684JSON\u6570\u636e\u9879\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528pandas\u5904\u7406JSON\u6570\u636e<\/h4>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5904\u7406JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8bJSON\u6570\u636e<\/strong><\/h2>\n<p>json_data = &#39;[{&quot;id&quot;: 1, &quot;name&quot;: &quot;John&quot;}, {&quot;id&quot;: 2, &quot;name&quot;: &quot;Jane&quot;}, {&quot;id&quot;: 3, &quot;name&quot;: &quot;Doe&quot;}]&#39;<\/p>\n<h2><strong>\u4f7f\u7528pandas\u5904\u7406JSON\u6570\u636e<\/strong><\/h2>\n<p>df = pd.read_json(json_data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528pandas\u7684<code>read_json()<\/code>\u65b9\u6cd5\u5c06JSON\u6570\u636e\u52a0\u8f7d\u4e3aDataFrame\uff0c\u5e76\u8fdb\u884c\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4f7f\u7528Python\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6JSON\u6570\u636e\u7684\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\uff0c\u5305\u62ec<strong>requests\u5e93\u3001BeautifulSoup\u5e93\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u7b49\u3002\u6211\u4eec\u8fd8\u8ba8\u8bba\u4e86\u5982\u4f55\u5904\u7406\u590d\u6742\u7684JSON\u7ed3\u6784\u4ee5\u53ca\u5927\u89c4\u6a21JSON\u6570\u636e\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\u80fd\u591f\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u9ad8\u6548\u5730\u5904\u7406JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u7f51\u9875\u4e0a\u7684JSON\u6570\u636e\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u63d0\u53d6\u7f51\u9875\u4e0a\u7684JSON\u6570\u636e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>requests<\/code>\u5e93\u6765\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u7136\u540e\u4f7f\u7528<code>json<\/code>\u5e93\u5c06\u54cd\u5e94\u5185\u5bb9\u89e3\u6790\u4e3aPython\u5bf9\u8c61\u3002\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u5b89\u88c5\u4e86<code>requests<\/code>\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install requests<\/code>\u6765\u5b8c\u6210\u3002\u7136\u540e\uff0c\u60a8\u53ea\u9700\u53d1\u9001GET\u8bf7\u6c42\u5e76\u89e3\u6790\u8fd4\u56de\u7684JSON\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import requests\n\nresponse = requests.get(&#39;https:\/\/example.com\/api\/data&#39;)\ndata = response.json()  # \u89e3\u6790JSON\nprint(data)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5904\u7406\u5f02\u5e38\uff0c\u4ee5\u9632\u8bf7\u6c42\u5931\u8d25\u6216\u8fd4\u56de\u975eJSON\u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u63d0\u53d6\u7f51\u9875JSON\u6570\u636e\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u63d0\u53d6\u7f51\u9875JSON\u6570\u636e\u65f6\uff0c\u60a8\u9700\u8981\u5173\u6ce8\u51e0\u4e2a\u65b9\u9762\u3002\u9996\u5148\uff0c\u8981\u786e\u4fdd\u76ee\u6807\u7f51\u7ad9\u5141\u8bb8\u6570\u636e\u6293\u53d6\uff0c\u67e5\u770b\u5176<code>robots.txt<\/code>\u6587\u4ef6\u4ee5\u786e\u4fdd\u60a8\u9075\u5b88\u7f51\u7ad9\u89c4\u5219\u3002\u5176\u6b21\uff0c\u7f51\u7edc\u8bf7\u6c42\u53ef\u80fd\u4f1a\u53d7\u5230\u9650\u5236\uff0c\u4f8b\u5982\u9891\u7387\u9650\u5236\u6216\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\uff0c\u56e0\u6b64\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u8bf7\u8003\u8651\u6dfb\u52a0\u9002\u5f53\u7684\u5ef6\u8fdf\u6216\u4f7f\u7528\u4ee3\u7406\u3002\u6b64\u5916\uff0c\u67d0\u4e9b\u7f51\u7ad9\u53ef\u80fd\u4f1a\u8fd4\u56deJSON\u6570\u636e\u9700\u8981\u7279\u5b9a\u7684\u8bf7\u6c42\u5934\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>headers<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u63d0\u53d6JSON\u6570\u636e\u540e\u5982\u4f55\u5904\u7406\u548c\u5206\u6790\uff1f<\/strong><br \/>\u63d0\u53d6JSON\u6570\u636e\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>pandas<\/code>\u5e93\u8fdb\u884c\u6570\u636e\u5206\u6790\u3002\u5c06JSON\u6570\u636e\u8f6c\u6362\u4e3a<code>DataFrame<\/code>\u975e\u5e38\u7b80\u5355\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>pd.DataFrame(data)<\/code>\u6765\u5b9e\u73b0\u3002\u4e4b\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u5de5\u5177\u5bf9\u6570\u636e\u8fdb\u884c\u6df1\u5165\u5206\u6790\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u7ed8\u5236\u56fe\u8868\u3001\u8ba1\u7b97\u7edf\u8ba1\u6570\u636e\u6216\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u7b49\u3002\u786e\u4fdd\u6839\u636e\u6570\u636e\u7684\u7ed3\u6784\u6765\u9009\u62e9\u5408\u9002\u7684\u5206\u6790\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u5c06\u7f51\u9875JSON\u63d0\u53d6\u51fa\u6765 \u8981\u7528Python\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6JSON\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528requests\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1131483,"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\/1131472"}],"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=1131472"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131472\/revisions"}],"predecessor-version":[{"id":1131486,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131472\/revisions\/1131486"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1131483"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1131472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1131472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1131472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}