{"id":978028,"date":"2024-12-27T06:34:23","date_gmt":"2024-12-26T22:34:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/978028.html"},"modified":"2024-12-27T06:34:25","modified_gmt":"2024-12-26T22:34:25","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e6%a0%87%e7%ad%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/978028.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5220\u9664\u6807\u7b7e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203214\/3a05434c-5f17-47fa-b0a9-22e65de93cea.webp\" alt=\"python\u4e2d\u5982\u4f55\u5220\u9664\u6807\u7b7e\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5220\u9664\u6807\u7b7e\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a<strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u501f\u52a9HTML\u89e3\u6790\u5e93\u5982BeautifulSoup\u3001\u5229\u7528lxml\u5e93<\/strong>\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5feb\u901f\u5730\u5339\u914d\u548c\u66ff\u6362\u6389\u4e0d\u9700\u8981\u7684\u6807\u7b7e\u3002BeautifulSoup\u548clxml\u5219\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7075\u6d3b\u548c\u51c6\u786e\u7684\u89e3\u6790\u65b9\u6cd5\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u590d\u6742\u7684HTML\u7ed3\u6784\u3002<strong>\u63a8\u8350\u4f7f\u7528BeautifulSoup<\/strong>\uff0c\u56e0\u4e3a\u5b83\u6613\u4e8e\u4f7f\u7528\uff0c\u5e76\u4e14\u80fd\u591f\u5f88\u597d\u5730\u5904\u7406\u4e0d\u89c4\u8303\u7684HTML\u4ee3\u7801\u3002BeautifulSoup\u80fd\u591f\u901a\u8fc7\u7b80\u5355\u7684\u65b9\u6cd5\u627e\u5230\u548c\u5220\u9664\u6807\u7b7e\uff0c\u5e76\u4e14\u4fdd\u7559\u6807\u7b7e\u5185\u7684\u6587\u672c\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528BeautifulSoup\u5220\u9664HTML\u6807\u7b7e\u7684\u8be6\u7ec6\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5220\u9664\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u5339\u914d\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u6a21\u5f0f\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>re<\/code>\u6a21\u5757\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u8981\u5220\u9664HTML\u6807\u7b7e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165<code>re<\/code>\u6a21\u5757\u3002<\/li>\n<li>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u548c\u66ff\u6362HTML\u6807\u7b7e\u3002<\/li>\n<li>\u6e05\u7406\u540e\u7684\u6587\u672c\u4e2d\u5c06\u4e0d\u5305\u542b\u4efb\u4f55HTML\u6807\u7b7e\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def remove_html_tags(text):<\/p>\n<p>    clean = re.compile(&#39;&lt;.*?&gt;&#39;)<\/p>\n<p>    return re.sub(clean, &#39;&#39;, text)<\/p>\n<p>html_content = &quot;&lt;p&gt;This is a &lt;b&gt;bold&lt;\/b&gt; paragraph.&lt;\/p&gt;&quot;<\/p>\n<p>clean_text = remove_html_tags(html_content)<\/p>\n<p>print(clean_text)  # \u8f93\u51fa: This is a bold paragraph.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u800c\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5904\u7406HTML\u5e76\u4e0d\u662f\u6700\u4f73\u5b9e\u8df5\uff0c\u56e0\u4e3aHTML\u53ef\u80fd\u662f\u5d4c\u5957\u548c\u4e0d\u89c4\u5219\u7684\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f1a\u663e\u5f97\u529b\u4e0d\u4ece\u5fc3\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528BeautifulSoup\u5220\u9664\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u6d41\u884c\u7684Python\u5e93\uff0c\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u3002\u5b83\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u6570\u636e\u3002\u5220\u9664HTML\u6807\u7b7e\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5BeautifulSoup\u5e93\uff08\u5982\u679c\u672a\u5b89\u88c5\uff09\u548clxml\u89e3\u6790\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4 lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u89e3\u6790HTML\u6587\u6863\u5e76\u627e\u5230\u9700\u8981\u5220\u9664\u7684\u6807\u7b7e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u5220\u9664\u6807\u7b7e\uff0c\u4f46\u4fdd\u7559\u6807\u7b7e\u5185\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>html_content = &quot;&lt;p&gt;This is a &lt;b&gt;bold&lt;\/b&gt; paragraph.&lt;\/p&gt;&quot;<\/p>\n<p>soup = BeautifulSoup(html_content, &#39;lxml&#39;)<\/p>\n<h2><strong>\u901a\u8fc7find_all\u627e\u5230\u6240\u6709&lt;b&gt;\u6807\u7b7e<\/strong><\/h2>\n<p>for tag in soup.find_all(&#39;b&#39;):<\/p>\n<p>    tag.unwrap()  # \u5220\u9664\u6807\u7b7e\u4f46\u4fdd\u7559\u6807\u7b7e\u5185\u7684\u6587\u672c<\/p>\n<p>clean_text = soup.get_text()<\/p>\n<p>print(clean_text)  # \u8f93\u51fa: This is a bold paragraph.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528lxml\u5e93\u5220\u9664\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>lxml\u662f\u4e00\u4e2a\u6027\u80fd\u4f18\u826f\u3001\u529f\u80fd\u5f3a\u5927\u7684Python\u5e93\uff0c\u7528\u4e8e\u5904\u7406XML\u548cHTML\u6587\u6863\u3002\u4f7f\u7528lxml\u5220\u9664\u6807\u7b7e\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5lxml\u5e93\uff08\u5982\u679c\u672a\u5b89\u88c5\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528lxml\u89e3\u6790HTML\u6587\u6863\u5e76\u5220\u9664\u6807\u7b7e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from lxml import html<\/p>\n<p>html_content = &quot;&lt;p&gt;This is a &lt;b&gt;bold&lt;\/b&gt; paragraph.&lt;\/p&gt;&quot;<\/p>\n<p>tree = html.fromstring(html_content)<\/p>\n<h2><strong>\u4f7f\u7528XPath\u9009\u62e9\u5e76\u5220\u9664\u6807\u7b7e<\/strong><\/h2>\n<p>for element in tree.xpath(&#39;\/\/b&#39;):<\/p>\n<p>    element.drop_tag()  # \u5220\u9664\u6807\u7b7e\u4f46\u4fdd\u7559\u6807\u7b7e\u5185\u7684\u6587\u672c<\/p>\n<p>clean_text = tree.text_content()<\/p>\n<p>print(clean_text)  # \u8f93\u51fa: This is a bold paragraph.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1. \u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5\u9002\u5408\u7b80\u5355\u7684HTML\u7ed3\u6784\uff0c\u4f46\u5bf9\u4e8e\u590d\u6742\u7684\u5d4c\u5957HTML\uff0c\u5bb9\u6613\u51fa\u73b0\u95ee\u9898\u3002\u7531\u4e8eHTML\u7684\u590d\u6742\u6027\u548c\u4e0d\u89c4\u5219\u6027\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u65b9\u6cd5\u53ef\u80fd\u4f1a\u6f0f\u6389\u67d0\u4e9b\u6807\u7b7e\u6216\u8005\u8bef\u5220\u5185\u5bb9\u3002\u56e0\u6b64\uff0c\u5bf9\u4e8e\u590d\u6742\u7ed3\u6784\u7684HTML\uff0c\u4e0d\u63a8\u8350\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>2. BeautifulSoup\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>BeautifulSoup\u662f\u5904\u7406HTML\u7684\u6700\u4f73\u9009\u62e9\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684API\u6765\u89e3\u6790\u548c\u64cd\u4f5cHTML\u6587\u6863\u3002\u5b83\u80fd\u591f\u81ea\u52a8\u4fee\u590d\u4e0d\u89c4\u8303\u7684HTML\uff0c\u5e76\u4e14\u53ef\u4ee5\u8f7b\u677e\u5730\u904d\u5386\u3001\u641c\u7d22\u548c\u4fee\u6539DOM\u6811\u7ed3\u6784\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\uff0cBeautifulSoup\u662f\u4e00\u4e2a\u7406\u60f3\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h4>3. lxml\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>lxml\u63d0\u4f9b\u4e86\u9ad8\u6027\u80fd\u7684HTML\u548cXML\u5904\u7406\u80fd\u529b\uff0c\u9002\u5408\u9700\u8981\u5feb\u901f\u89e3\u6790\u548c\u5904\u7406\u5927\u91cfHTML\u6587\u6863\u7684\u573a\u666f\u3002\u5b83\u7684XPath\u652f\u6301\u4f7f\u5f97\u67e5\u627e\u548c\u64cd\u4f5cDOM\u6811\u53d8\u5f97\u975e\u5e38\u65b9\u4fbf\u3002\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u590d\u6742XPath\u67e5\u8be2\u7684\u60c5\u51b5\uff0clxml\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5220\u9664HTML\u6807\u7b7e\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u3002\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548cHTML\u7684\u590d\u6742\u6027\u3002<strong>\u5bf9\u4e8e\u7b80\u5355\u7684HTML\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5feb\u901f\u5b9e\u73b0\u6807\u7b7e\u7684\u5220\u9664<\/strong>\uff1b<strong>\u5bf9\u4e8e\u590d\u6742\u7684HTML\u7ed3\u6784\uff0cBeautifulSoup\u548clxml\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u89e3\u51b3\u65b9\u6848<\/strong>\u3002\u5728\u8fdb\u884c\u6587\u672c\u5904\u7406\u65f6\uff0c\u786e\u4fdd\u9009\u62e9\u7684\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u591f\u9ad8\u6548\u5730\u5220\u9664\u6807\u7b7e\uff0c\u8fd8\u80fd\u4fdd\u8bc1\u6587\u672c\u5185\u5bb9\u7684\u5b8c\u6574\u6027\u548c\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5220\u9664HTML\u6807\u7b7e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5220\u9664HTML\u6807\u7b7e\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u5e93\uff0c\u8fd9\u662f\u4e00\u4e2a\u5f3a\u5927\u7684HTML\u89e3\u6790\u5e93\u3002\u60a8\u53ea\u9700\u5b89\u88c5BeautifulSoup\uff0c\u5e76\u4f7f\u7528\u5b83\u7684<code>decompose()<\/code>\u6216<code>extract()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u6307\u5b9a\u7684\u6807\u7b7e\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a  <\/p>\n<pre><code class=\"language-python\">from bs4 import BeautifulSoup\n\nhtml_content = &quot;&lt;div&gt;Hello &lt;span&gt;World&lt;\/span&gt;&lt;\/div&gt;&quot;\nsoup = BeautifulSoup(html_content, &#39;html.parser&#39;)\nfor span in soup.find_all(&#39;span&#39;):\n    span.decompose()  # \u6216\u8005\u4f7f\u7528 span.extract()\nprint(soup)  # \u8f93\u51fa: &lt;div&gt;Hello &lt;\/div&gt;\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6807\u7b7e\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528BeautifulSoup\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6807\u7b7e\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\n\nhtml_content = &quot;&lt;div&gt;Hello &lt;span&gt;World&lt;\/span&gt;&lt;\/div&gt;&quot;\ncleaned_text = re.sub(r&#39;&lt;.*?&gt;&#39;, &#39;&#39;, html_content)  # \u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u9664\u6240\u6709\u6807\u7b7e\nprint(cleaned_text)  # \u8f93\u51fa: Hello World\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6807\u7b7e\u5220\u9664\uff0c\u4f46\u5904\u7406\u590d\u6742HTML\u65f6\u53ef\u80fd\u4f1a\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n<p><strong>\u5728\u5220\u9664\u6807\u7b7e\u540e\uff0c\u5982\u4f55\u4fdd\u7559\u6807\u7b7e\u5185\u7684\u6587\u672c\u5185\u5bb9\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u7684\u76ee\u6807\u662f\u5220\u9664\u6807\u7b7e\u4f46\u4fdd\u7559\u6807\u7b7e\u5185\u7684\u6587\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u7684<code>get_text()<\/code>\u65b9\u6cd5\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u53d6\u6240\u6709\u6587\u672c\u5e76\u5ffd\u7565\u6807\u7b7e\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from bs4 import BeautifulSoup\n\nhtml_content = &quot;&lt;div&gt;Hello &lt;span&gt;World&lt;\/span&gt;&lt;\/div&gt;&quot;\nsoup = BeautifulSoup(html_content, &#39;html.parser&#39;)\ntext_content = soup.get_text()  # \u83b7\u53d6\u6807\u7b7e\u5185\u7684\u6587\u672c\nprint(text_content)  # \u8f93\u51fa: Hello World\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u7b80\u5355\u800c\u6709\u6548\uff0c\u786e\u4fdd\u60a8\u83b7\u5f97\u7eaf\u6587\u672c\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5220\u9664\u6807\u7b7e\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u501f\u52a9HTML\u89e3\u6790\u5e93\u5982BeautifulSoup\u3001\u5229\u7528l [&hellip;]","protected":false},"author":3,"featured_media":978035,"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\/978028"}],"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=978028"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/978028\/revisions"}],"predecessor-version":[{"id":978040,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/978028\/revisions\/978040"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/978035"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=978028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=978028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=978028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}