{"id":984352,"date":"2024-12-27T07:27:24","date_gmt":"2024-12-26T23:27:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/984352.html"},"modified":"2024-12-27T07:27:27","modified_gmt":"2024-12-26T23:27:27","slug":"python%e5%a6%82%e4%bd%95%e5%8e%bb%e6%8e%89%e5%ad%90%e6%a0%87%e7%ad%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/984352.html","title":{"rendered":"python\u5982\u4f55\u53bb\u6389\u5b50\u6807\u7b7e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212015\/cbd11fae-aca4-4060-98d3-8dd55d171295.webp\" alt=\"python\u5982\u4f55\u53bb\u6389\u5b50\u6807\u7b7e\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u53bb\u6389\u5b50\u6807\u7b7e\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u5e93\u8fdb\u884c\u89e3\u6790\u3001\u901a\u8fc7\u9012\u5f52\u6216\u8fed\u4ee3\u7684\u65b9\u5f0f\u5220\u9664\u5b50\u6807\u7b7e\u3001\u5e76\u91cd\u65b0\u7ec4\u5408\u7236\u6807\u7b7e\u7684\u5185\u5bb9\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u9012\u5f52\u5220\u9664\u5b50\u6807\u7b7e\u7684\u65b9\u6cd5\u6700\u4e3a\u5e38\u89c1\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u7075\u6d3b\u5904\u7406\u5404\u79cd\u590d\u6742\u7684HTML\u7ed3\u6784\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u53bb\u6389\u5b50\u6807\u7b7e\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u89e3\u6790HTML\u6587\u6863<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406HTML\u6587\u6863\u65f6\uff0c\u9996\u5148\u9700\u8981\u89e3\u6790\u6587\u6863\u5185\u5bb9\u3002Python\u4e2d\u7684BeautifulSoup\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684HTML\u548cXML\u89e3\u6790\u5de5\u5177\u3002\u901a\u8fc7BeautifulSoup\uff0c\u53ef\u4ee5\u8f7b\u677e\u89e3\u6790HTML\u6587\u6863\uff0c\u83b7\u53d6\u9700\u8981\u7684\u6807\u7b7e\u548c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5BeautifulSoup<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5BeautifulSoup\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7pip\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<ol start=\"2\">\n<li>\u89e3\u6790HTML\u6587\u6863<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u6587\u6863\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u76ee\u6807\u6807\u7b7e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>html_doc = &quot;&quot;&quot;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>    &lt;body&gt;<\/p>\n<p>        &lt;div&gt;<\/p>\n<p>            &lt;p&gt;\u8fd9\u662f\u4e00\u4e2a\u6bb5\u843d\u3002&lt;\/p&gt;<\/p>\n<p>            &lt;span&gt;\u8fd9\u662f\u4e00\u4e2a&lt;span&gt;\u5d4c\u5957&lt;\/span&gt;\u7684\u6807\u7b7e\u3002&lt;\/span&gt;<\/p>\n<p>        &lt;\/div&gt;<\/p>\n<p>    &lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>soup = BeautifulSoup(html_doc, &#39;html.parser&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u6bb5\u5c06HTML\u6587\u6863\u89e3\u6790\u4e3a\u4e00\u4e2aBeautifulSoup\u5bf9\u8c61\uff0c\u4fbf\u4e8e\u8fdb\u4e00\u6b65\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u53bb\u6389\u5b50\u6807\u7b7e<\/p>\n<\/p>\n<p><p>\u53bb\u6389\u5b50\u6807\u7b7e\u7684\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u9009\u62e9\uff0c\u4ee5\u4e0b\u4ecb\u7ecd\u51e0\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u9012\u5f52\u5220\u9664\u5b50\u6807\u7b7e<\/li>\n<\/ol>\n<p><p>\u9012\u5f52\u5220\u9664\u5b50\u6807\u7b7e\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b83\u80fd\u591f\u5904\u7406\u591a\u5c42\u5d4c\u5957\u7684HTML\u7ed3\u6784\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_child_tags(soup):<\/p>\n<p>    for child in soup.find_all():<\/p>\n<p>        if child.children:<\/p>\n<p>            remove_child_tags(child)<\/p>\n<p>        child.unwrap()<\/p>\n<p>remove_child_tags(soup)<\/p>\n<p>print(soup.prettify())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>remove_child_tags<\/code>\u51fd\u6570\u9012\u5f52\u5904\u7406\u6240\u6709\u5b50\u6807\u7b7e\uff0c\u5e76\u4f7f\u7528<code>unwrap<\/code>\u65b9\u6cd5\u79fb\u9664\u5b83\u4eec\uff0c\u4ece\u800c\u4fdd\u7559\u6587\u672c\u5185\u5bb9\u3002\u6700\u7ec8\u7ed3\u679c\u662f\u4e00\u4e2a\u6ca1\u6709\u5b50\u6807\u7b7e\u7684HTML\u6587\u6863\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528CSS\u9009\u62e9\u5668<\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528CSS\u9009\u62e9\u5668\u76f4\u63a5\u9009\u62e9\u5e76\u5220\u9664\u7279\u5b9a\u7684\u5b50\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for span in soup.select(&#39;span span&#39;):<\/p>\n<p>    span.unwrap()<\/p>\n<p>print(soup.prettify())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65b9\u6cd5\u9002\u7528\u4e8e\u5df2\u77e5\u7ed3\u6784\u8f83\u4e3a\u7b80\u5355\u7684HTML\u6587\u6863\uff0c\u901a\u8fc7CSS\u9009\u62e9\u5668\u76f4\u63a5\u5b9a\u4f4d\u5230\u76ee\u6807\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5408\u5e76\u6587\u672c\u5185\u5bb9<\/p>\n<\/p>\n<p><p>\u53bb\u6389\u5b50\u6807\u7b7e\u540e\uff0c\u53ef\u80fd\u9700\u8981\u5408\u5e76\u6587\u672c\u5185\u5bb9\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>.stripped_strings<\/code>\u65b9\u6cd5<\/li>\n<\/ol>\n<p><p>BeautifulSoup\u63d0\u4f9b\u7684<code>.stripped_strings<\/code>\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u6807\u7b7e\u4e2d\u7684\u7eaf\u6587\u672c\u5185\u5bb9\uff0c\u5e76\u53bb\u6389\u591a\u4f59\u7684\u7a7a\u767d\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &#39; &#39;.join(soup.stripped_strings)<\/p>\n<p>print(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u6bb5\u5c06\u5408\u5e76\u6240\u6709\u6587\u672c\u5185\u5bb9\uff0c\u5e76\u53bb\u6389\u591a\u4f59\u7684\u7a7a\u767d\u5b57\u7b26\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u624b\u52a8\u5408\u5e76\u6587\u672c<\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u624b\u52a8\u5408\u5e76\u6587\u672c\u5185\u5bb9\uff0c\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_text_without_tags(soup):<\/p>\n<p>    if not soup.contents:<\/p>\n<p>        return &#39;&#39;<\/p>\n<p>    text = &#39;&#39;<\/p>\n<p>    for content in soup.contents:<\/p>\n<p>        if content.name is None:<\/p>\n<p>            text += content<\/p>\n<p>        else:<\/p>\n<p>            text += get_text_without_tags(content)<\/p>\n<p>    return text<\/p>\n<p>text = get_text_without_tags(soup)<\/p>\n<p>print(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65b9\u6cd5\u901a\u8fc7\u9012\u5f52\u904d\u5386\u83b7\u53d6\u6240\u6709\u6587\u672c\u8282\u70b9\uff0c\u5e76\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5e38\u89c1\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848<\/p>\n<\/p>\n<p><p>\u5728\u53bb\u6389\u5b50\u6807\u7b7e\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4ee5\u4e0b\u95ee\u9898\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6807\u7b7e\u5d4c\u5957\u8fc7\u6df1<\/strong>\uff1a\u5bf9\u4e8e\u6807\u7b7e\u5d4c\u5957\u8fc7\u6df1\u7684HTML\u6587\u6863\uff0c\u9012\u5f52\u65b9\u6cd5\u53ef\u80fd\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f18\u5316\u9012\u5f52\u7b97\u6cd5\u6216\u4f7f\u7528\u8fed\u4ee3\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u590d\u6742\u7684CSS\u9009\u62e9\u5668<\/strong>\uff1a\u5bf9\u4e8e\u590d\u6742\u7684CSS\u9009\u62e9\u5668\uff0c\u53ef\u80fd\u9700\u8981\u624b\u52a8\u7f16\u5199\u9009\u62e9\u5668\u4ee5\u51c6\u786e\u5b9a\u4f4d\u76ee\u6807\u6807\u7b7e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u7559\u7279\u5b9a\u6807\u7b7e<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u4fdd\u7559\u7279\u5b9a\u7684\u5b50\u6807\u7b7e\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u5728\u5220\u9664\u5b50\u6807\u7b7e\u4e4b\u524d\u8fdb\u884c\u5224\u65ad\uff0c\u8df3\u8fc7\u9700\u8981\u4fdd\u7559\u7684\u6807\u7b7e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5b9e\u8df5\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u53bb\u6389\u5b50\u6807\u7b7e\u7684\u64cd\u4f5c\u5728\u6570\u636e\u5904\u7406\u548c\u7f51\u9875\u89e3\u6790\u4e2d\u975e\u5e38\u5e38\u89c1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u6e05\u6d17<\/strong>\uff1a\u5728\u4ece\u7f51\u9875\u63d0\u53d6\u6570\u636e\u65f6\uff0c\u53bb\u6389\u4e0d\u5fc5\u8981\u7684\u5b50\u6807\u7b7e\u6709\u52a9\u4e8e\u7b80\u5316\u6570\u636e\u7ed3\u6784\uff0c\u63d0\u9ad8\u6570\u636e\u8d28\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6587\u672c\u5206\u6790<\/strong>\uff1a\u5728\u8fdb\u884c\u6587\u672c\u5206\u6790\u65f6\uff0c\u9700\u8981\u53bb\u6389HTML\u6807\u7b7e\u4ee5\u83b7\u5f97\u7eaf\u6587\u672c\u5185\u5bb9\uff0c\u4ee5\u4fbf\u8fdb\u884c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f51\u9875\u5185\u5bb9\u63d0\u53d6<\/strong>\uff1a\u5728\u8fdb\u884c\u7f51\u9875\u5185\u5bb9\u63d0\u53d6\u65f6\uff0c\u53bb\u6389\u5b50\u6807\u7b7e\u80fd\u591f\u83b7\u5f97\u66f4\u4e3a\u7b80\u6d01\u7684\u7f51\u9875\u5185\u5bb9\uff0c\u63d0\u9ad8\u4fe1\u606f\u63d0\u53d6\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0cPython\u80fd\u591f\u9ad8\u6548\u53bb\u6389HTML\u6587\u6863\u4e2d\u7684\u5b50\u6807\u7b7e\uff0c\u5e76\u83b7\u53d6\u6240\u9700\u7684\u6587\u672c\u5185\u5bb9\u3002\u8fd9\u5728\u6570\u636e\u5904\u7406\u3001\u7f51\u9875\u89e3\u6790\u548c\u6587\u672c\u5206\u6790\u7b49\u9886\u57df\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u53bb\u6389HTML\u4e2d\u7684\u5b50\u6807\u7b7e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u5e93\u6765\u89e3\u6790HTML\u6587\u6863\u5e76\u53bb\u6389\u6307\u5b9a\u7684\u5b50\u6807\u7b7e\u3002\u901a\u8fc7\u9009\u62e9\u7236\u6807\u7b7e\u5e76\u8c03\u7528<code>decompose()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5220\u9664\u5b50\u6807\u7b7e\u3002\u786e\u4fdd\u5728\u64cd\u4f5c\u4e4b\u524d\u5b89\u88c5BeautifulSoup\u5e93\uff0c\u5e76\u5bfc\u5165\u76f8\u5e94\u7684\u6a21\u5757\u3002<\/p>\n<p><strong>\u53bb\u6389\u5b50\u6807\u7b7e\u5bf9\u539f\u6587\u6863\u6709\u5f71\u54cd\u5417\uff1f<\/strong><br \/>\u53bb\u6389\u5b50\u6807\u7b7e\u4f1a\u76f4\u63a5\u4fee\u6539HTML\u6587\u6863\u7684\u7ed3\u6784\uff0c\u56e0\u6b64\u5728\u5904\u7406\u91cd\u8981\u6570\u636e\u65f6\uff0c\u8981\u6ce8\u610f\u5907\u4efd\u539f\u59cb\u6587\u6863\u3002\u5220\u9664\u5b50\u6807\u7b7e\u540e\uff0c\u76f8\u5173\u7684\u6837\u5f0f\u548c\u811a\u672c\u53ef\u80fd\u4e5f\u4f1a\u53d7\u5230\u5f71\u54cd\uff0c\u56e0\u6b64\u5728\u8fdb\u884c\u4fee\u6539\u65f6\u9700\u8981\u4ed4\u7ec6\u8003\u8651\u3002<\/p>\n<p><strong>\u9664\u4e86BeautifulSoup\uff0c\u8fd8\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u53bb\u6389\u5b50\u6807\u7b7e\uff1f<\/strong><br \/>\u9664\u4e86BeautifulSoup\uff0cPython\u7684lxml\u5e93\u4e5f\u53ef\u4ee5\u7528\u6765\u5904\u7406HTML\u548cXML\u6587\u6863\u3002\u901a\u8fc7XPath\u6216CSS\u9009\u62e9\u5668\uff0c\u53ef\u4ee5\u7cbe\u786e\u9009\u62e9\u8981\u5220\u9664\u7684\u5b50\u6807\u7b7e\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u4f7f\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u53bb\u6389\u5b50\u6807\u7b7e\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u5e93\u8fdb\u884c\u89e3\u6790\u3001\u901a\u8fc7\u9012\u5f52\u6216\u8fed\u4ee3\u7684\u65b9\u5f0f\u5220\u9664\u5b50\u6807\u7b7e\u3001\u5e76\u91cd\u65b0 [&hellip;]","protected":false},"author":3,"featured_media":984359,"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\/984352"}],"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=984352"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/984352\/revisions"}],"predecessor-version":[{"id":984364,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/984352\/revisions\/984364"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/984359"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=984352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=984352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=984352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}