{"id":1050563,"date":"2024-12-31T14:09:02","date_gmt":"2024-12-31T06:09:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1050563.html"},"modified":"2024-12-31T14:09:04","modified_gmt":"2024-12-31T06:09:04","slug":"python%e5%a6%82%e4%bd%95%e5%be%aa%e7%8e%af%e4%b8%80%e6%ac%a1%e7%9f%a5%e4%b9%8e","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1050563.html","title":{"rendered":"python\u5982\u4f55\u5faa\u73af\u4e00\u6b21\u77e5\u4e4e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/bfd1816f-7ab2-4415-8246-bcf817162115.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5faa\u73af\u4e00\u6b21\u77e5\u4e4e\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u77e5\u4e4e\u7684API\u548c\u5faa\u73af\u7ed3\u6784\u6765\u5b9e\u73b0\u5faa\u73af\u4e00\u6b21\u77e5\u4e4e\u7684\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5229\u7528\u77e5\u4e4eAPI\u3001\u4f7f\u7528requests\u6a21\u5757\u3001\u5904\u7406JSON\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u70b9\uff0c\u8ba9\u6211\u4eec\u5c55\u5f00\u8bb2\u89e3\u5982\u4f55\u5229\u7528requests\u6a21\u5757\u4e0e\u77e5\u4e4eAPI\u8fdb\u884c\u4ea4\u4e92\u3002requests\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\u7684\u5e93\uff0c\u5b83\u53ef\u4ee5\u8f7b\u677e\u5730\u5411\u77e5\u4e4eAPI\u53d1\u9001\u8bf7\u6c42\u5e76\u83b7\u53d6\u54cd\u5e94\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528requests\u6a21\u5757\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5requests\u6a21\u5757<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5requests\u6a21\u5757\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528requests\u6a21\u5757\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u5e76\u83b7\u53d6\u77e5\u4e4eAPI\u7684\u54cd\u5e94\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;https:\/\/www.zhihu.com\/api\/v4\/questions\/1234567890\/answers&#39;  # \u793a\u4f8b\u77e5\u4e4eAPI\u63a5\u53e3<\/p>\n<p>headers = {<\/p>\n<p>    &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.124 Safari\/537.36&#39;<\/p>\n<p>}<\/p>\n<p>response = requests.get(url, headers=headers)<\/p>\n<p>data = response.json()<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406JSON\u6570\u636e<\/strong><\/p>\n<p>\u77e5\u4e4eAPI\u7684\u54cd\u5e94\u6570\u636e\u901a\u5e38\u662fJSON\u683c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684json\u6a21\u5757\u89e3\u6790\u8fd9\u4e9b\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = json.loads(response.text)<\/p>\n<p>for answer in data[&#39;data&#39;]:<\/p>\n<p>    print(answer[&#39;content&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e00\u3001\u77e5\u4e4eAPI\u7684\u57fa\u7840\u77e5\u8bc6<\/h3>\n<\/p>\n<p><p>\u77e5\u4e4eAPI\u662f\u4e00\u4e2a\u57fa\u4e8eREST\u67b6\u6784\u7684\u63a5\u53e3\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u901a\u8fc7HTTP\u8bf7\u6c42\u83b7\u53d6\u77e5\u4e4e\u4e0a\u7684\u5404\u79cd\u6570\u636e\uff0c\u5982\u95ee\u9898\u3001\u7b54\u6848\u3001\u7528\u6237\u4fe1\u606f\u7b49\u3002\u4e3a\u4e86\u4f7f\u7528\u77e5\u4e4eAPI\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3\u4e00\u4e9b\u57fa\u672c\u7684\u6982\u5ff5\u548c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>API\u7aef\u70b9<\/strong>\uff1aAPI\u7aef\u70b9\u662f\u6307API\u7684URL\u5730\u5740\uff0c\u4f8b\u5982\uff1a<a href=\"https:\/\/www.zhihu.com\/api\/v4\/questions\/1234567890\/answers\">https:\/\/www.zhihu.com\/api\/v4\/questions\/1234567890\/answers<\/a><\/li>\n<li><strong>\u8bf7\u6c42\u65b9\u6cd5<\/strong>\uff1a\u5e38\u89c1\u7684\u8bf7\u6c42\u65b9\u6cd5\u6709GET\u3001POST\u3001PUT\u3001DELETE\u7b49\u3002\u6211\u4eec\u901a\u5e38\u4f7f\u7528GET\u65b9\u6cd5\u6765\u83b7\u53d6\u6570\u636e\u3002<\/li>\n<li><strong>\u8bf7\u6c42\u5934<\/strong>\uff1a\u8bf7\u6c42\u5934\u5305\u542b\u4e86\u4e00\u4e9b\u5ba2\u6237\u7aef\u7684\u4fe1\u606f\uff0c\u5982User-Agent\u3001Authorization\u7b49\uff0c\u6709\u4e9bAPI\u9700\u8981\u643a\u5e26\u7279\u5b9a\u7684\u8bf7\u6c42\u5934\u4fe1\u606f\u3002<\/li>\n<li><strong>\u54cd\u5e94\u6570\u636e<\/strong>\uff1aAPI\u7684\u54cd\u5e94\u6570\u636e\u901a\u5e38\u662fJSON\u683c\u5f0f\uff0c\u6211\u4eec\u9700\u8981\u89e3\u6790\u8fd9\u4e9b\u6570\u636e\u4ee5\u83b7\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u5982\u4f55\u83b7\u53d6\u77e5\u4e4eAPI\u7684\u6388\u6743<\/h3>\n<\/p>\n<p><p>\u6709\u4e9b\u77e5\u4e4eAPI\u9700\u8981\u7528\u6237\u6388\u6743\u624d\u80fd\u8bbf\u95ee\uff0c\u8fd9\u65f6\u6211\u4eec\u9700\u8981\u83b7\u53d6OAuth 2.0\u6388\u6743\u4ee4\u724c\u3002\u4ee5\u4e0b\u662f\u83b7\u53d6\u6388\u6743\u4ee4\u724c\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6ce8\u518c\u5e94\u7528<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u77e5\u4e4e\u5f00\u53d1\u8005\u5e73\u53f0\u6ce8\u518c\u4e00\u4e2a\u5e94\u7528\uff0c\u83b7\u53d6client_id\u548cclient_secret\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u6388\u6743\u7801<\/strong><\/p>\n<p>\u4f7f\u7528\u7528\u6237\u7684\u77e5\u4e4e\u8d26\u53f7\u767b\u5f55\uff0c\u5e76\u83b7\u53d6\u6388\u6743\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8bURL\uff1a<\/p>\n<\/p>\n<p><pre><code>https:\/\/www.zhihu.com\/oauth\/authorize?client_id=your_client_id&amp;redirect_uri=your_redirect_uri&amp;response_type=code&amp;scope=read<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u8bbf\u95ee\u4ee4\u724c<\/strong><\/p>\n<p>\u4f7f\u7528\u6388\u6743\u7801\u83b7\u53d6\u8bbf\u95ee\u4ee4\u724c\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;https:\/\/www.zhihu.com\/oauth\/token&#39;<\/p>\n<p>data = {<\/p>\n<p>    &#39;client_id&#39;: &#39;your_client_id&#39;,<\/p>\n<p>    &#39;client_secret&#39;: &#39;your_client_secret&#39;,<\/p>\n<p>    &#39;grant_type&#39;: &#39;authorization_code&#39;,<\/p>\n<p>    &#39;code&#39;: &#39;your_authorization_code&#39;,<\/p>\n<p>    &#39;redirect_uri&#39;: &#39;your_redirect_uri&#39;<\/p>\n<p>}<\/p>\n<p>response = requests.post(url, data=data)<\/p>\n<p>access_token = response.json()[&#39;access_token&#39;]<\/p>\n<p>print(access_token)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u5229\u7528Python\u5faa\u73af\u83b7\u53d6\u77e5\u4e4e\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528Python\u5faa\u73af\u83b7\u53d6\u77e5\u4e4e\u4e0a\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u901a\u8fc7\u5faa\u73af\u83b7\u53d6\u67d0\u4e2a\u95ee\u9898\u7684\u6240\u6709\u56de\u7b54\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def get_answers(question_id, offset=0, limit=20):<\/p>\n<p>    url = f&#39;https:\/\/www.zhihu.com\/api\/v4\/questions\/{question_id}\/answers&#39;<\/p>\n<p>    headers = {<\/p>\n<p>        &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.124 Safari\/537.36&#39;<\/p>\n<p>    }<\/p>\n<p>    params = {<\/p>\n<p>        &#39;offset&#39;: offset,<\/p>\n<p>        &#39;limit&#39;: limit,<\/p>\n<p>        &#39;sort_by&#39;: &#39;default&#39;<\/p>\n<p>    }<\/p>\n<p>    response = requests.get(url, headers=headers, params=params)<\/p>\n<p>    return response.json()<\/p>\n<p>question_id = &#39;1234567890&#39;  # \u793a\u4f8b\u95ee\u9898ID<\/p>\n<p>offset = 0<\/p>\n<p>limit = 20<\/p>\n<p>while True:<\/p>\n<p>    data = get_answers(question_id, offset, limit)<\/p>\n<p>    answers = data[&#39;data&#39;]<\/p>\n<p>    if not answers:<\/p>\n<p>        break<\/p>\n<p>    for answer in answers:<\/p>\n<p>        print(answer[&#39;content&#39;])<\/p>\n<p>    offset += limit<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u77e5\u4e4eAPI\u7684\u5206\u9875\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u77e5\u4e4eAPI\u7684\u5f88\u591a\u6570\u636e\u90fd\u662f\u5206\u9875\u8fd4\u56de\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5904\u7406\u5206\u9875\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u901a\u8fc7\u5faa\u73af\u83b7\u53d6\u67d0\u4e2a\u95ee\u9898\u7684\u6240\u6709\u56de\u7b54\uff0c\u76f4\u5230\u6ca1\u6709\u66f4\u591a\u6570\u636e\u4e3a\u6b62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def get_answers(question_id, offset=0, limit=20):<\/p>\n<p>    url = f&#39;https:\/\/www.zhihu.com\/api\/v4\/questions\/{question_id}\/answers&#39;<\/p>\n<p>    headers = {<\/p>\n<p>        &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.124 Safari\/537.36&#39;<\/p>\n<p>    }<\/p>\n<p>    params = {<\/p>\n<p>        &#39;offset&#39;: offset,<\/p>\n<p>        &#39;limit&#39;: limit,<\/p>\n<p>        &#39;sort_by&#39;: &#39;default&#39;<\/p>\n<p>    }<\/p>\n<p>    response = requests.get(url, headers=headers, params=params)<\/p>\n<p>    return response.json()<\/p>\n<p>question_id = &#39;1234567890&#39;  # \u793a\u4f8b\u95ee\u9898ID<\/p>\n<p>offset = 0<\/p>\n<p>limit = 20<\/p>\n<p>while True:<\/p>\n<p>    data = get_answers(question_id, offset, limit)<\/p>\n<p>    answers = data[&#39;data&#39;]<\/p>\n<p>    if not answers:<\/p>\n<p>        break<\/p>\n<p>    for answer in answers:<\/p>\n<p>        print(answer[&#39;content&#39;])<\/p>\n<p>    offset += limit<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u77e5\u4e4eAPI\u7684\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u77e5\u4e4eAPI\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u8bf7\u6c42\u5931\u8d25\u3001\u6570\u636e\u683c\u5f0f\u9519\u8bef\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bf7\u6c42\u5931\u8d25<\/strong><\/p>\n<p>\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u8bf7\u6c42\u5931\u8d25\u7684\u60c5\u51b5\uff0c\u5982\u7f51\u7edc\u95ee\u9898\u3001API\u9650\u5236\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6765\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def get_answers(question_id, offset=0, limit=20):<\/p>\n<p>    url = f&#39;https:\/\/www.zhihu.com\/api\/v4\/questions\/{question_id}\/answers&#39;<\/p>\n<p>    headers = {<\/p>\n<p>        &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.124 Safari\/537.36&#39;<\/p>\n<p>    }<\/p>\n<p>    params = {<\/p>\n<p>        &#39;offset&#39;: offset,<\/p>\n<p>        &#39;limit&#39;: limit,<\/p>\n<p>        &#39;sort_by&#39;: &#39;default&#39;<\/p>\n<p>    }<\/p>\n<p>    try:<\/p>\n<p>        response = requests.get(url, headers=headers, params=params)<\/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(f&quot;\u8bf7\u6c42\u5931\u8d25\uff1a{e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>    return response.json()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u683c\u5f0f\u9519\u8bef<\/strong><\/p>\n<p>\u5728\u89e3\u6790API\u54cd\u5e94\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6570\u636e\u683c\u5f0f\u9519\u8bef\u7684\u60c5\u51b5\uff0c\u5982JSON\u89e3\u6790\u9519\u8bef\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6765\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>import json<\/p>\n<p>def get_answers(question_id, offset=0, limit=20):<\/p>\n<p>    url = f&#39;https:\/\/www.zhihu.com\/api\/v4\/questions\/{question_id}\/answers&#39;<\/p>\n<p>    headers = {<\/p>\n<p>        &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.124 Safari\/537.36&#39;<\/p>\n<p>    }<\/p>\n<p>    params = {<\/p>\n<p>        &#39;offset&#39;: offset,<\/p>\n<p>        &#39;limit&#39;: limit,<\/p>\n<p>        &#39;sort_by&#39;: &#39;default&#39;<\/p>\n<p>    }<\/p>\n<p>    try:<\/p>\n<p>        response = requests.get(url, headers=headers, params=params)<\/p>\n<p>        response.raise_for_status()<\/p>\n<p>        data = response.json()<\/p>\n<p>    except requests.exceptions.RequestException as e:<\/p>\n<p>        print(f&quot;\u8bf7\u6c42\u5931\u8d25\uff1a{e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>    except json.JSONDecodeError as e:<\/p>\n<p>        print(f&quot;JSON\u89e3\u6790\u9519\u8bef\uff1a{e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>    return data<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u4f18\u5316\u77e5\u4e4e\u6570\u636e\u7684\u5904\u7406\u6d41\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u83b7\u53d6\u5230\u7684\u6570\u636e\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u548c\u4f18\u5316\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4f18\u5316\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u5b58\u50a8<\/strong><\/p>\n<p>\u5728\u83b7\u53d6\u5230\u77e5\u4e4e\u7684\u6570\u636e\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u5b58\u50a8\u5230\u6570\u636e\u5e93\u6216\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\u548c\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u6570\u636e\u5b58\u50a8\u5230SQLite\u6570\u636e\u5e93\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>import requests<\/p>\n<p>def create_table():<\/p>\n<p>    conn = sqlite3.connect(&#39;zhihu.db&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS answers<\/p>\n<p>                      (id INTEGER PRIMARY KEY,<\/p>\n<p>                       content TEXT)&#39;&#39;&#39;)<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p>def insert_answer(content):<\/p>\n<p>    conn = sqlite3.connect(&#39;zhihu.db&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;INSERT INTO answers (content) VALUES (?)&quot;, (content,))<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p>def get_answers(question_id, offset=0, limit=20):<\/p>\n<p>    url = f&#39;https:\/\/www.zhihu.com\/api\/v4\/questions\/{question_id}\/answers&#39;<\/p>\n<p>    headers = {<\/p>\n<p>        &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.124 Safari\/537.36&#39;<\/p>\n<p>    }<\/p>\n<p>    params = {<\/p>\n<p>        &#39;offset&#39;: offset,<\/p>\n<p>        &#39;limit&#39;: limit,<\/p>\n<p>        &#39;sort_by&#39;: &#39;default&#39;<\/p>\n<p>    }<\/p>\n<p>    try:<\/p>\n<p>        response = requests.get(url, headers=headers, params=params)<\/p>\n<p>        response.raise_for_status()<\/p>\n<p>        data = response.json()<\/p>\n<p>    except requests.exceptions.RequestException as e:<\/p>\n<p>        print(f&quot;\u8bf7\u6c42\u5931\u8d25\uff1a{e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>    return data<\/p>\n<p>question_id = &#39;1234567890&#39;  # \u793a\u4f8b\u95ee\u9898ID<\/p>\n<p>offset = 0<\/p>\n<p>limit = 20<\/p>\n<p>create_table()<\/p>\n<p>while True:<\/p>\n<p>    data = get_answers(question_id, offset, limit)<\/p>\n<p>    if data is None:<\/p>\n<p>        break<\/p>\n<p>    answers = data[&#39;data&#39;]<\/p>\n<p>    if not answers:<\/p>\n<p>        break<\/p>\n<p>    for answer in answers:<\/p>\n<p>        insert_answer(answer[&#39;content&#39;])<\/p>\n<p>    offset += limit<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u5206\u6790<\/strong><\/p>\n<p>\u5728\u83b7\u53d6\u5230\u77e5\u4e4e\u7684\u6570\u636e\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684pandas\u5e93\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u6790\u548c\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u5206\u6790\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import requests<\/p>\n<p>def get_answers(question_id, offset=0, limit=20):<\/p>\n<p>    url = f&#39;https:\/\/www.zhihu.com\/api\/v4\/questions\/{question_id}\/answers&#39;<\/p>\n<p>    headers = {<\/p>\n<p>        &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.124 Safari\/537.36&#39;<\/p>\n<p>    }<\/p>\n<p>    params = {<\/p>\n<p>        &#39;offset&#39;: offset,<\/p>\n<p>        &#39;limit&#39;: limit,<\/p>\n<p>        &#39;sort_by&#39;: &#39;default&#39;<\/p>\n<p>    }<\/p>\n<p>    try:<\/p>\n<p>        response = requests.get(url, headers=headers, params=params)<\/p>\n<p>        response.raise_for_status()<\/p>\n<p>        data = response.json()<\/p>\n<p>    except requests.exceptions.RequestException as e:<\/p>\n<p>        print(f&quot;\u8bf7\u6c42\u5931\u8d25\uff1a{e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>    return data<\/p>\n<p>question_id = &#39;1234567890&#39;  # \u793a\u4f8b\u95ee\u9898ID<\/p>\n<p>offset = 0<\/p>\n<p>limit = 20<\/p>\n<p>all_answers = []<\/p>\n<p>while True:<\/p>\n<p>    data = get_answers(question_id, offset, limit)<\/p>\n<p>    if data is None:<\/p>\n<p>        break<\/p>\n<p>    answers = data[&#39;data&#39;]<\/p>\n<p>    if not answers:<\/p>\n<p>        break<\/p>\n<p>    for answer in answers:<\/p>\n<p>        all_answers.append(answer[&#39;content&#39;])<\/p>\n<p>    offset += limit<\/p>\n<p>df = pd.DataFrame(all_answers, columns=[&#39;content&#39;])<\/p>\n<p>print(df.describe())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528Python\u5faa\u73af\u83b7\u53d6\u77e5\u4e4e\u4e0a\u7684\u6570\u636e\uff0c\u5e76\u5bf9\u6570\u636e\u8fdb\u884c\u5b58\u50a8\u548c\u5206\u6790\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u9700\u8981\u8fdb\u4e00\u6b65\u7684\u5e2e\u52a9\uff0c\u8bf7\u968f\u65f6\u8054\u7cfb\u6211\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u5bf9\u77e5\u4e4e\u5185\u5bb9\u7684\u5faa\u73af\u8bbf\u95ee\uff1f<\/strong><br \/>\u8981\u4f7f\u7528Python\u5faa\u73af\u8bbf\u95ee\u77e5\u4e4e\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f51\u7edc\u722c\u866b\u5e93\u5982Requests\u548cBeautifulSoup\u3002\u901a\u8fc7\u6784\u9020URL\u5e76\u53d1\u9001\u8bf7\u6c42\uff0c\u89e3\u6790\u8fd4\u56de\u7684HTML\u5185\u5bb9\uff0c\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u53ef\u4ee5\u7ed3\u5408\u5faa\u73af\u7ed3\u6784\uff0c\u6301\u7eed\u8bbf\u95ee\u4e0d\u540c\u7684\u9875\u9762\u6216\u5185\u5bb9\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u81ea\u52a8\u6293\u53d6\u3002<\/p>\n<p><strong>\u5728\u5faa\u73af\u8bbf\u95ee\u77e5\u4e4e\u65f6\uff0c\u5982\u4f55\u5904\u7406\u53cd\u722c\u866b\u63aa\u65bd\uff1f<\/strong><br \/>\u77e5\u4e4e\u5bf9\u4e8e\u9891\u7e41\u7684\u8bf7\u6c42\u6709\u4e00\u5b9a\u7684\u53cd\u722c\u866b\u673a\u5236\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8bf7\u6c42\u5934\u3001\u4f7f\u7528\u4ee3\u7406IP\u548c\u9002\u5f53\u5ef6\u8fdf\u8bf7\u6c42\u6765\u964d\u4f4e\u88ab\u5c01\u7981\u7684\u98ce\u9669\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u968f\u673a\u7684User-Agent\u4e5f\u80fd\u5e2e\u52a9\u6a21\u62df\u771f\u5b9e\u7528\u6237\u7684\u8bbf\u95ee\u884c\u4e3a\uff0c\u4ece\u800c\u63d0\u9ad8\u722c\u53d6\u7684\u6210\u529f\u7387\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u6293\u53d6\u77e5\u4e4e\u6570\u636e\u65f6\uff0c\u6709\u54ea\u4e9b\u5e93\u63a8\u8350\uff1f<\/strong><br \/>\u5728\u6293\u53d6\u77e5\u4e4e\u6570\u636e\u65f6\uff0c\u63a8\u8350\u4f7f\u7528Requests\u5e93\u8fdb\u884cHTTP\u8bf7\u6c42\uff0cBeautifulSoup\u6216lxml\u8fdb\u884cHTML\u89e3\u6790\uff0cPandas\u7528\u4e8e\u6570\u636e\u5904\u7406\u548c\u5b58\u50a8\u3002\u6b64\u5916\uff0cScrapy\u6846\u67b6\u4e5f\u5f88\u9002\u5408\u8fdb\u884c\u5927\u89c4\u6a21\u7684\u6570\u636e\u6293\u53d6\uff0c\u80fd\u591f\u7b80\u5316\u5904\u7406\u6d41\u7a0b\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u77e5\u4e4e\u7684API\u548c\u5faa\u73af\u7ed3\u6784\u6765\u5b9e\u73b0\u5faa\u73af\u4e00\u6b21\u77e5\u4e4e\u7684\u64cd\u4f5c\u3002 \u5229\u7528\u77e5\u4e4eAPI\u3001\u4f7f\u7528reque [&hellip;]","protected":false},"author":3,"featured_media":1050570,"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\/1050563"}],"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=1050563"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1050563\/revisions"}],"predecessor-version":[{"id":1050573,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1050563\/revisions\/1050573"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1050570"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1050563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1050563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1050563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}