{"id":1082721,"date":"2025-01-08T12:51:30","date_gmt":"2025-01-08T04:51:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1082721.html"},"modified":"2025-01-08T12:51:32","modified_gmt":"2025-01-08T04:51:32","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%88%ac%e5%8f%96%e8%80%83%e7%a0%94%e7%9c%9f%e9%a2%98-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1082721.html","title":{"rendered":"\u5982\u4f55\u7528python\u722c\u53d6\u8003\u7814\u771f\u9898"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24184111\/5a552f10-df2d-441a-83fc-14e6bd141d77.webp\" alt=\"\u5982\u4f55\u7528python\u722c\u53d6\u8003\u7814\u771f\u9898\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u722c\u53d6\u8003\u7814\u771f\u9898\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u9009\u62e9\u5408\u9002\u7684\u76ee\u6807\u7f51\u7ad9\u3001\u5206\u6790\u7f51\u9875\u7ed3\u6784\u3001\u6a21\u62dfHTTP\u8bf7\u6c42\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3001\u4fdd\u5b58\u6570\u636e<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u76ee\u6807\u7f51\u7ad9<br \/>\u5728\u5f00\u59cb\u7f16\u5199\u722c\u866b\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u9009\u62e9\u4e00\u4e2a\u5305\u542b\u8003\u7814\u771f\u9898\u7684\u7f51\u7ad9\u3002\u9009\u62e9\u7f51\u7ad9\u65f6\uff0c\u5e94\u786e\u4fdd\u8be5\u7f51\u7ad9\u7684\u5185\u5bb9\u4e30\u5bcc\u4e14\u7ed3\u6784\u7a33\u5b9a\u3002\u53ef\u4ee5\u901a\u8fc7\u7f51\u7edc\u641c\u7d22\u627e\u5230\u4e00\u4e9b\u63d0\u4f9b\u8003\u7814\u771f\u9898\u7684\u7f51\u7ad9\uff0c\u5982\u8003\u7814\u5e2e\u3001\u8003\u7814\u8bba\u575b\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5206\u6790\u7f51\u9875\u7ed3\u6784<br \/>\u4e86\u89e3\u76ee\u6807\u7f51\u7ad9\u7684\u7f51\u9875\u7ed3\u6784\u662f\u7f16\u5199\u722c\u866b\u7684\u5173\u952e\u3002\u4f7f\u7528\u6d4f\u89c8\u5668\u7684\u5f00\u53d1\u8005\u5de5\u5177\uff08F12\uff09\u67e5\u770b\u7f51\u9875\u7684HTML\u4ee3\u7801\uff0c\u627e\u51fa\u5305\u542b\u771f\u9898\u5185\u5bb9\u7684\u5143\u7d20\u53ca\u5176\u8def\u5f84\u3002\u901a\u5e38\uff0c\u771f\u9898\u4f1a\u4ee5\u8868\u683c\u3001\u5217\u8868\u6216\u6bb5\u843d\u7684\u5f62\u5f0f\u5c55\u793a\u5728\u7f51\u9875\u4e0a\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6a21\u62dfHTTP\u8bf7\u6c42<br \/>\u4f7f\u7528Python\u7684requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002requests\u5e93\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684HTTP\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u53d1\u9001GET\u548cPOST\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;\u76ee\u6807\u7f51\u7ad9\u7684URL&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>html_content = response.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9<br \/>\u4f7f\u7528BeautifulSoup\u5e93\u89e3\u6790HTML\u5185\u5bb9\uff0c\u4ece\u4e2d\u63d0\u53d6\u51fa\u9700\u8981\u7684\u771f\u9898\u4fe1\u606f\u3002BeautifulSoup\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684HTML\u89e3\u6790\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u6807\u7b7e\u540d\u3001\u7c7b\u540d\u3001ID\u7b49\u65b9\u5f0f\u5b9a\u4f4d\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u6839\u636e\u7f51\u9875\u7ed3\u6784\u627e\u5230\u5305\u542b\u771f\u9898\u7684\u5143\u7d20<\/strong><\/h2>\n<p>questions = soup.find_all(&#39;\u5177\u4f53\u7684\u6807\u7b7e\u6216\u7c7b\u540d&#39;)<\/p>\n<p>for question in questions:<\/p>\n<p>    print(question.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4fdd\u5b58\u6570\u636e<br \/>\u5c06\u722c\u53d6\u5230\u7684\u771f\u9898\u4fe1\u606f\u4fdd\u5b58\u5230\u672c\u5730\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u3002\u53ef\u4ee5\u9009\u62e9\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6\u3001CSV\u6587\u4ef6\u6216\u5b58\u5165\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;kaoyan_zhenti.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    for question in questions:<\/p>\n<p>        file.write(question.text + &#39;\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u76ee\u6807\u7f51\u7ad9<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u76ee\u6807\u7f51\u7ad9\u662f\u722c\u53d6\u8003\u7814\u771f\u9898\u7684\u7b2c\u4e00\u6b65\u3002\u6211\u4eec\u9700\u8981\u786e\u4fdd\u7f51\u7ad9\u7684\u5185\u5bb9\u4e30\u5bcc\u4e14\u7f51\u9875\u7ed3\u6784\u7a33\u5b9a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u8003\u7814\u771f\u9898\u7f51\u7ad9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8003\u7814\u5e2e<\/strong>\uff1a\u8003\u7814\u5e2e\u662f\u4e00\u4e2a\u63d0\u4f9b\u8003\u7814\u4fe1\u606f\u548c\u8d44\u6599\u7684\u7f51\u7ad9\uff0c\u5305\u542b\u4e86\u5927\u91cf\u7684\u8003\u7814\u771f\u9898\u548c\u89e3\u6790\u3002<\/li>\n<li><strong>\u8003\u7814\u8bba\u575b<\/strong>\uff1a\u8003\u7814\u8bba\u575b\u662f\u4e00\u4e2a\u4ea4\u6d41\u8003\u7814\u7ecf\u9a8c\u548c\u5206\u4eab\u8d44\u6599\u7684\u793e\u533a\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230\u8bb8\u591a\u8003\u7814\u771f\u9898\u3002<\/li>\n<li><strong>\u5b66\u6821\u5b98\u7f51<\/strong>\uff1a\u4e00\u4e9b\u9ad8\u6821\u5b98\u7f51\u4e5f\u4f1a\u53d1\u5e03\u5386\u5e74\u7684\u8003\u7814\u771f\u9898\uff0c\u7279\u522b\u662f\u81ea\u4e3b\u547d\u9898\u7684\u5b66\u6821\u3002<\/li>\n<\/ol>\n<p><p>\u5728\u9009\u62e9\u76ee\u6807\u7f51\u7ad9\u65f6\uff0c\u5e94\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5185\u5bb9\u4e30\u5bcc<\/strong>\uff1a\u7f51\u7ad9\u4e0a\u5e94\u5305\u542b\u591a\u5e74\u7684\u8003\u7814\u771f\u9898\uff0c\u4ee5\u4fbf\u83b7\u53d6\u8db3\u591f\u7684\u6570\u636e\u8fdb\u884c\u5206\u6790\u3002<\/li>\n<li><strong>\u7f51\u9875\u7ed3\u6784\u7a33\u5b9a<\/strong>\uff1a\u7f51\u7ad9\u7684\u7f51\u9875\u7ed3\u6784\u5e94\u76f8\u5bf9\u7a33\u5b9a\uff0c\u4ee5\u907f\u514d\u9891\u7e41\u4fee\u6539\u722c\u866b\u4ee3\u7801\u3002<\/li>\n<li><strong>\u5408\u6cd5\u6027<\/strong>\uff1a\u786e\u4fdd\u722c\u53d6\u884c\u4e3a\u5408\u6cd5\uff0c\u4e0d\u4fb5\u72af\u7f51\u7ad9\u7684\u7248\u6743\u548c\u9690\u79c1\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u5206\u6790\u7f51\u9875\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u786e\u5b9a\u76ee\u6807\u7f51\u7ad9\u540e\uff0c\u9700\u8981\u5206\u6790\u7f51\u9875\u7684\u7ed3\u6784\uff0c\u4ee5\u4fbf\u7f16\u5199\u722c\u866b\u4ee3\u7801\u3002\u4f7f\u7528\u6d4f\u89c8\u5668\u7684\u5f00\u53d1\u8005\u5de5\u5177\uff08F12\uff09\u67e5\u770b\u7f51\u9875\u7684HTML\u4ee3\u7801\uff0c\u627e\u51fa\u5305\u542b\u771f\u9898\u5185\u5bb9\u7684\u5143\u7d20\u53ca\u5176\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5728\u8003\u7814\u5e2e\u7f51\u7ad9\u4e0a\uff0c\u8003\u7814\u771f\u9898\u53ef\u80fd\u4ee5\u5217\u8868\u7684\u5f62\u5f0f\u5c55\u793a\u3002\u6211\u4eec\u53ef\u4ee5\u53f3\u952e\u70b9\u51fb\u771f\u9898\u5185\u5bb9\uff0c\u9009\u62e9\u201c\u68c0\u67e5\u5143\u7d20\u201d\uff0c\u67e5\u770bHTML\u4ee3\u7801\u3002\u5047\u8bbe\u771f\u9898\u5185\u5bb9\u5728\u4e00\u4e2a<code>&lt;div&gt;<\/code>\u6807\u7b7e\u5185\uff0c\u7c7b\u540d\u4e3a<code>exam-content<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u5e93\u5b9a\u4f4d\u8fd9\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;div class=&quot;exam-content&quot;&gt;<\/p>\n<p>    &lt;p&gt;2019\u5e74\u8003\u7814\u6570\u5b66\u771f\u9898&lt;\/p&gt;<\/p>\n<p>    &lt;p&gt;...&lt;\/p&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5206\u6790\u7f51\u9875\u7ed3\u6784\u65f6\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6807\u7b7e\u540d<\/strong>\uff1a\u4e0d\u540c\u7f51\u7ad9\u4f7f\u7528\u7684\u6807\u7b7e\u540d\u53ef\u80fd\u4e0d\u540c\uff0c\u5982<code>&lt;div&gt;<\/code>\u3001<code>&lt;p&gt;<\/code>\u3001<code>&lt;span&gt;<\/code>\u7b49\u3002<\/li>\n<li><strong>\u7c7b\u540d\u548cID<\/strong>\uff1a\u7c7b\u540d\u548cID\u662f\u5b9a\u4f4d\u5143\u7d20\u7684\u91cd\u8981\u4f9d\u636e\uff0c\u53ef\u4ee5\u901a\u8fc7\u7c7b\u540d\u548cID\u5feb\u901f\u627e\u5230\u76ee\u6807\u5143\u7d20\u3002<\/li>\n<li><strong>\u5c42\u7ea7\u5173\u7cfb<\/strong>\uff1a\u4e00\u4e9b\u771f\u9898\u5185\u5bb9\u53ef\u80fd\u5d4c\u5957\u5728\u591a\u4e2a\u6807\u7b7e\u5185\uff0c\u9700\u8981\u4e86\u89e3\u5b83\u4eec\u7684\u5c42\u7ea7\u5173\u7cfb\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u6a21\u62dfHTTP\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>\u5728\u5206\u6790\u5b8c\u7f51\u9875\u7ed3\u6784\u540e\uff0c\u9700\u8981\u4f7f\u7528Python\u7684requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>requests\u5e93\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684HTTP\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u53d1\u9001GET\u548cPOST\u8bf7\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;\u76ee\u6807\u7f51\u7ad9\u7684URL&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>html_content = response.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u53d1\u9001HTTP\u8bf7\u6c42\u65f6\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u8bf7\u6c42\u5934<\/strong>\uff1a\u6709\u4e9b\u7f51\u7ad9\u4f1a\u5bf9\u8bf7\u6c42\u5934\u8fdb\u884c\u68c0\u67e5\uff0c\u4ee5\u9632\u6b62\u722c\u866b\u8bbf\u95ee\u3002\u53ef\u4ee5\u5728requests\u5e93\u4e2d\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff0c\u6a21\u62df\u6d4f\u89c8\u5668\u8bbf\u95ee\u3002<\/li>\n<li><strong>Cookies<\/strong>\uff1a\u6709\u4e9b\u7f51\u7ad9\u9700\u8981\u767b\u5f55\u624d\u80fd\u8bbf\u95ee\u771f\u9898\u5185\u5bb9\uff0c\u53ef\u4ee5\u5728requests\u5e93\u4e2d\u8bbe\u7f6eCookies\uff0c\u4fdd\u6301\u767b\u5f55\u72b6\u6001\u3002<\/li>\n<li><strong>\u4ee3\u7406<\/strong>\uff1a\u4e3a\u4e86\u907f\u514d\u88ab\u5c01IP\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee3\u7406\u53d1\u9001\u8bf7\u6c42\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\u540e\uff0c\u9700\u8981\u4f7f\u7528BeautifulSoup\u5e93\u89e3\u6790HTML\u5185\u5bb9\uff0c\u4ece\u4e2d\u63d0\u53d6\u51fa\u9700\u8981\u7684\u771f\u9898\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684HTML\u89e3\u6790\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u6807\u7b7e\u540d\u3001\u7c7b\u540d\u3001ID\u7b49\u65b9\u5f0f\u5b9a\u4f4d\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528BeautifulSoup\u5e93\u89e3\u6790HTML\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u6839\u636e\u7f51\u9875\u7ed3\u6784\u627e\u5230\u5305\u542b\u771f\u9898\u7684\u5143\u7d20<\/strong><\/h2>\n<p>questions = soup.find_all(&#39;div&#39;, class_=&#39;exam-content&#39;)<\/p>\n<p>for question in questions:<\/p>\n<p>    print(question.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u65f6\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u591a\u5c42\u5d4c\u5957<\/strong>\uff1a\u6709\u4e9b\u771f\u9898\u5185\u5bb9\u53ef\u80fd\u5d4c\u5957\u5728\u591a\u4e2a\u6807\u7b7e\u5185\uff0c\u9700\u8981\u9010\u5c42\u89e3\u6790\u3002<\/li>\n<li><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7279\u5b9a\u683c\u5f0f\u7684\u5185\u5bb9\uff0c\u5982\u5e74\u4efd\u3001\u79d1\u76ee\u7b49\u3002<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406<\/strong>\uff1a\u5728\u89e3\u6790\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u6807\u7b7e\u7f3a\u5931\u3001\u683c\u5f0f\u4e0d\u4e00\u81f4\u7b49\uff0c\u9700\u8981\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u4fdd\u5b58\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5c06\u722c\u53d6\u5230\u7684\u771f\u9898\u4fe1\u606f\u4fdd\u5b58\u5230\u672c\u5730\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u3002\u53ef\u4ee5\u9009\u62e9\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6\u3001CSV\u6587\u4ef6\u6216\u5b58\u5165\u6570\u636e\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4fdd\u5b58\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6<\/strong>\uff1a\u5c06\u771f\u9898\u4fe1\u606f\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6\uff0c\u65b9\u4fbf\u67e5\u770b\u548c\u7f16\u8f91\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">with open(&#39;kaoyan_zhenti.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    for question in questions:<\/p>\n<p>        file.write(question.text + &#39;\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6<\/strong>\uff1a\u5c06\u771f\u9898\u4fe1\u606f\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\uff0c\u65b9\u4fbf\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;kaoyan_zhenti.csv&#39;, &#39;w&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as csvfile:<\/p>\n<p>    writer = csv.writer(csvfile)<\/p>\n<p>    for question in questions:<\/p>\n<p>        writer.writerow([question.text])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5b58\u5165\u6570\u636e\u5e93<\/strong>\uff1a\u5c06\u771f\u9898\u4fe1\u606f\u5b58\u5165\u6570\u636e\u5e93\uff0c\u65b9\u4fbf\u67e5\u8be2\u548c\u7ba1\u7406\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>conn = sqlite3.connect(&#39;kaoyan_zhenti.db&#39;)<\/p>\n<p>c = conn.cursor()<\/p>\n<p>c.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS zhenti (id INTEGER PRIMARY KEY, content TEXT)&#39;&#39;&#39;)<\/p>\n<p>for question in questions:<\/p>\n<p>    c.execute(&quot;INSERT INTO zhenti (content) VALUES (?)&quot;, (question.text,))<\/p>\n<p>conn.commit()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4fdd\u5b58\u6570\u636e\u65f6\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6570\u636e\u683c\u5f0f<\/strong>\uff1a\u786e\u4fdd\u4fdd\u5b58\u7684\u6570\u636e\u683c\u5f0f\u4e00\u81f4\uff0c\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u3002<\/li>\n<li><strong>\u53bb\u91cd<\/strong>\uff1a\u907f\u514d\u4fdd\u5b58\u91cd\u590d\u7684\u771f\u9898\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u6216\u6570\u636e\u5e93\u7684\u552f\u4e00\u7ea6\u675f\u3002<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406<\/strong>\uff1a\u5728\u4fdd\u5b58\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u6587\u4ef6\u6743\u9650\u4e0d\u8db3\u3001\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\u7b49\uff0c\u9700\u8981\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528Python\u722c\u53d6\u8003\u7814\u771f\u9898\uff0c\u5e76\u4fdd\u5b58\u5230\u672c\u5730\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>def get_html(url):<\/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\/58.0.3029.110 Safari\/537.36&#39;<\/p>\n<p>    }<\/p>\n<p>    response = requests.get(url, headers=headers)<\/p>\n<p>    return response.text<\/p>\n<p>def parse_html(html):<\/p>\n<p>    soup = BeautifulSoup(html, &#39;html.parser&#39;)<\/p>\n<p>    questions = soup.find_all(&#39;div&#39;, class_=&#39;exam-content&#39;)<\/p>\n<p>    return [question.text for question in questions]<\/p>\n<p>def save_to_file(data, filename):<\/p>\n<p>    with open(filename, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        for item in data:<\/p>\n<p>            file.write(item + &#39;\\n&#39;)<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    url = &#39;\u76ee\u6807\u7f51\u7ad9\u7684URL&#39;<\/p>\n<p>    html = get_html(url)<\/p>\n<p>    questions = parse_html(html)<\/p>\n<p>    save_to_file(questions, &#39;kaoyan_zhenti.txt&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e09\u4e2a\u51fd\u6570\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>get_html<\/strong>\uff1a\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002<\/li>\n<li><strong>parse_html<\/strong>\uff1a\u89e3\u6790HTML\u5185\u5bb9\uff0c\u63d0\u53d6\u771f\u9898\u4fe1\u606f\u3002<\/li>\n<li><strong>save_to_file<\/strong>\uff1a\u5c06\u771f\u9898\u4fe1\u606f\u4fdd\u5b58\u5230\u672c\u5730\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><p>\u5728\u4e3b\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\uff0c\u7136\u540e\u89e3\u6790HTML\u5185\u5bb9\uff0c\u6700\u540e\u5c06\u771f\u9898\u4fe1\u606f\u4fdd\u5b58\u5230\u672c\u5730\u6587\u4ef6\u3002\u8fd9\u6837\uff0c\u5c31\u5b8c\u6210\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u8003\u7814\u771f\u9898\u722c\u866b\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u8fdb\u9636\u6280\u5de7<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u66f4\u590d\u6742\u7684\u60c5\u51b5\uff0c\u5982\u52a8\u6001\u52a0\u8f7d\u3001\u9a8c\u8bc1\u7801\u3001\u53cd\u722c\u866b\u673a\u5236\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8fdb\u9636\u6280\u5de7\uff0c\u5e2e\u52a9\u5e94\u5bf9\u8fd9\u4e9b\u6311\u6218\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5904\u7406\u52a8\u6001\u52a0\u8f7d<\/strong>\uff1a\u6709\u4e9b\u7f51\u7ad9\u4f7f\u7528JavaScript\u52a8\u6001\u52a0\u8f7d\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\uff0c\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>driver = webdriver.Chrome()<\/p>\n<p>driver.get(&#39;\u76ee\u6807\u7f51\u7ad9\u7684URL&#39;)<\/p>\n<p>html = driver.page_source<\/p>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7834\u89e3\u9a8c\u8bc1\u7801<\/strong>\uff1a\u6709\u4e9b\u7f51\u7ad9\u4f7f\u7528\u9a8c\u8bc1\u7801\u9632\u6b62\u722c\u866b\uff0c\u53ef\u4ee5\u4f7f\u7528\u56fe\u50cf\u8bc6\u522b\u6280\u672f\u7834\u89e3\u9a8c\u8bc1\u7801\uff0c\u5982Tesseract OCR\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import pytesseract<\/p>\n<p>image = Image.open(&#39;captcha.png&#39;)<\/p>\n<p>text = pytesseract.image_to_string(image)<\/p>\n<p>print(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5e94\u5bf9\u53cd\u722c\u866b\u673a\u5236<\/strong>\uff1a\u6709\u4e9b\u7f51\u7ad9\u4f1a\u901a\u8fc7IP\u5c01\u7981\u3001\u8bf7\u6c42\u9891\u7387\u9650\u5236\u7b49\u624b\u6bb5\u9632\u6b62\u722c\u866b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee3\u7406\u3001\u968f\u673a\u5ef6\u65f6\u7b49\u65b9\u5f0f\u5e94\u5bf9\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import random<\/p>\n<p>proxies = {<\/p>\n<p>    &#39;http&#39;: &#39;http:\/\/\u4ee3\u7406IP:\u7aef\u53e3&#39;,<\/p>\n<p>    &#39;https&#39;: &#39;https:\/\/\u4ee3\u7406IP:\u7aef\u53e3&#39;<\/p>\n<p>}<\/p>\n<p>response = requests.get(url, proxies=proxies)<\/p>\n<p>time.sleep(random.uniform(1, 3))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u8fdb\u9636\u6280\u5de7\uff0c\u53ef\u4ee5\u5e94\u5bf9\u66f4\u590d\u6742\u7684\u722c\u866b\u9700\u6c42\uff0c\u63d0\u9ad8\u722c\u53d6\u6548\u7387\u548c\u6210\u529f\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684Python\u5e93\u8fdb\u884c\u8003\u7814\u771f\u9898\u7684\u722c\u53d6\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u722c\u53d6\u8003\u7814\u771f\u9898\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u81f3\u5173\u91cd\u8981\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecRequests\u548cBeautiful Soup\u3002Requests\u5e93\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u800cBeautiful Soup\u5219\u7528\u4e8e\u89e3\u6790HTML\u6587\u6863\uff0c\u63d0\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002\u6b64\u5916\uff0cScrapy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6846\u67b6\uff0c\u9002\u5408\u5927\u89c4\u6a21\u722c\u866b\u9879\u76ee\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u7ba1\u7406\u8bf7\u6c42\u548c\u6570\u636e\u5b58\u50a8\u3002<\/p>\n<p><strong>\u722c\u53d6\u8003\u7814\u771f\u9898\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6cd5\u5f8b\u548c\u9053\u5fb7\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u8003\u7814\u771f\u9898\u722c\u53d6\u65f6\uff0c\u9075\u5faa\u6cd5\u5f8b\u6cd5\u89c4\u548c\u9053\u5fb7\u6807\u51c6\u975e\u5e38\u91cd\u8981\u3002\u786e\u4fdd\u5c0a\u91cd\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\uff0c\u4e86\u89e3\u54ea\u4e9b\u5185\u5bb9\u5141\u8bb8\u722c\u53d6\u3002\u540c\u65f6\uff0c\u8981\u907f\u514d\u5bf9\u7f51\u7ad9\u9020\u6210\u8fc7\u5927\u7684\u8bf7\u6c42\u538b\u529b\uff0c\u5408\u7406\u8bbe\u7f6e\u722c\u53d6\u9891\u7387\u3002\u6b64\u5916\uff0c\u5207\u52ff\u5c06\u722c\u53d6\u7684\u6570\u636e\u7528\u4e8e\u5546\u4e1a\u76ee\u7684\uff0c\u5c0a\u91cd\u539f\u4f5c\u8005\u7684\u7248\u6743\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u722c\u53d6\u5230\u7684\u8003\u7814\u771f\u9898\u6570\u636e\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u5206\u6790\uff1f<\/strong><br \/>\u83b7\u53d6\u8003\u7814\u771f\u9898\u540e\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u6574\u7406\uff0c\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u5c06\u6570\u636e\u8f6c\u6362\u4e3aDataFrame\u683c\u5f0f\uff0c\u4fbf\u4e8e\u64cd\u4f5c\u548c\u5206\u6790\u3002\u5728\u6570\u636e\u6e05\u6d17\u8fc7\u7a0b\u4e2d\uff0c\u53bb\u9664\u91cd\u590d\u9879\u3001\u5904\u7406\u7f3a\u5931\u503c\u548c\u6807\u51c6\u5316\u6570\u636e\u683c\u5f0f\u7b49\u6b65\u9aa4\u90fd\u662f\u5fc5\u8981\u7684\u3002\u6574\u7406\u540e\u7684\u6570\u636e\u53ef\u4ee5\u4fdd\u5b58\u4e3aCSV\u6216Excel\u683c\u5f0f\uff0c\u4ee5\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\u548c\u5206\u4eab\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u722c\u53d6\u8003\u7814\u771f\u9898\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u9009\u62e9\u5408\u9002\u7684\u76ee\u6807\u7f51\u7ad9\u3001\u5206\u6790\u7f51\u9875\u7ed3\u6784\u3001\u6a21\u62dfHTTP\u8bf7\u6c42\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1082732,"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\/1082721"}],"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=1082721"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082721\/revisions"}],"predecessor-version":[{"id":1082733,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082721\/revisions\/1082733"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1082732"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1082721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1082721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1082721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}