{"id":938275,"date":"2024-12-26T20:08:22","date_gmt":"2024-12-26T12:08:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/938275.html"},"modified":"2024-12-26T20:08:24","modified_gmt":"2024-12-26T12:08:24","slug":"python%e5%a6%82%e4%bd%95%e5%8e%bb%e6%8e%89-p","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/938275.html","title":{"rendered":"python\u5982\u4f55\u53bb\u6389< p>"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073827\/69c40960-b87d-4ac6-8148-6d58ed9ef2ff.webp\" alt=\"python\u5982\u4f55\u53bb\u6389< p>&#8221; \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u53bb\u6389<code>&lt;p&gt;<\/code>\u6807\u7b7e\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001HTML\u89e3\u6790\u5e93BeautifulSoup\u3001\u5b57\u7b26\u4e32\u66ff\u6362\u7b49\u65b9\u6cd5\u3002<\/strong>\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528BeautifulSoup\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u53bb\u6389\u7279\u5b9a\u7684HTML\u6807\u7b7e\uff0c\u8fd8\u80fd\u89e3\u6790\u548c\u5904\u7406HTML\u6587\u6863\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\u6765\u53bb\u6389<code>&lt;p&gt;<\/code>\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u6587\u6863\uff0c\u53ef\u4ee5\u6709\u6548\u53bb\u9664\u7279\u5b9a\u6807\u7b7e\uff0c\u6bd4\u5982<code>&lt;p&gt;<\/code>\u6807\u7b7e\u3002BeautifulSoup\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Python\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u89e3\u6790\u548c\u5904\u7406HTML\u548cXML\u6587\u6863\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u53bb\u9664\u6307\u5b9a\u7684\u6807\u7b7e\uff0c\u8fd8\u80fd\u4fdd\u6301\u6587\u6863\u7684\u7ed3\u6784\u5b8c\u6574\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528BeautifulSoup\u6765\u53bb\u6389<code>&lt;p&gt;<\/code>\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528BeautifulSoup\u53bb\u9664<code>&lt;p&gt;<\/code>\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2aPython\u5e93\uff0c\u7528\u4e8e\u4eceHTML\u548cXML\u6587\u4ef6\u4e2d\u63d0\u53d6\u6570\u636e\u3002\u5b83\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684\u89e3\u6790\u5668\u6765\u89e3\u6790\u6587\u4ef6\uff0c\u6bd4\u5982lxml\u3001html.parser\u7b49\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528BeautifulSoup\u53bb\u9664<code>&lt;p&gt;<\/code>\u6807\u7b7e\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5BeautifulSoup<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5BeautifulSoup\u5e93\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4<\/p>\n<p>pip install lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u89e3\u6790HTML\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aHTML\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528BeautifulSoup\u6765\u89e3\u6790\u5b83\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;head&gt;<\/p>\n<p>    &lt;title&gt;Example Page&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;p&gt;This is a paragraph.&lt;\/p&gt;<\/p>\n<p>    &lt;div&gt;Here is some &lt;p&gt;text with a paragraph tag&lt;\/p&gt; inside a div.&lt;\/div&gt;<\/p>\n<p>    &lt;p&gt;Another paragraph.&lt;\/p&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;lxml&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u53bb\u9664<code>&lt;p&gt;<\/code>\u6807\u7b7e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u7684<code>.decompose()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u6240\u6709\u7684<code>&lt;p&gt;<\/code>\u6807\u7b7e\uff0c\u540c\u65f6\u4fdd\u7559\u5176\u5185\u90e8\u7684\u6587\u672c\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for p in soup.find_all(&#39;p&#39;):<\/p>\n<p>    p.unwrap()<\/p>\n<p>print(soup.prettify())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>find_all(&#39;p&#39;)<\/code>\u65b9\u6cd5\u627e\u5230\u6240\u6709\u7684<code>&lt;p&gt;<\/code>\u6807\u7b7e\uff0c\u7136\u540e\u901a\u8fc7<code>unwrap()<\/code>\u65b9\u6cd5\u6765\u53bb\u6389\u8fd9\u4e9b\u6807\u7b7e\uff0c\u4f46\u4fdd\u7559\u5176\u4e2d\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528BeautifulSoup\uff0c\u8fd8\u6709\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u53bb\u6389<code>&lt;p&gt;<\/code>\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u4e8e\u67e5\u627e\u548c\u66ff\u6362\u6587\u672c\u5185\u5bb9\u3002\u867d\u7136\u4e0d\u63a8\u8350\u76f4\u63a5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u89e3\u6790HTML\uff0c\u4f46\u5728\u67d0\u4e9b\u7b80\u5355\u573a\u666f\u4e0b\uff0c\u5b83\u53ef\u4ee5\u662f\u4e00\u4e2a\u5feb\u6377\u7684\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>html_doc = &quot;&quot;&quot;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Example Page&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;p&gt;This is a paragraph.&lt;\/p&gt;<\/p>\n<p>    &lt;div&gt;Here is some &lt;p&gt;text with a paragraph tag&lt;\/p&gt; inside a div.&lt;\/div&gt;<\/p>\n<p>    &lt;p&gt;Another paragraph.&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<h2><strong>\u53bb\u6389&lt;p&gt;\u6807\u7b7e<\/strong><\/h2>\n<p>clean_html = re.sub(r&#39;&lt;\\\/?p&gt;&#39;, &#39;&#39;, html_doc)<\/p>\n<p>print(clean_html)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<code>re.sub(r&#39;&lt;\\\/?p&gt;&#39;, &#39;&#39;, html_doc)<\/code>\u6765\u53bb\u9664<code>&lt;p&gt;<\/code>\u6807\u7b7e\u3002\u8fd9\u4e00\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u4e0d\u5efa\u8bae\u7528\u4e8e\u590d\u6742HTML\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u66ff\u6362\u662f\u6700\u7b80\u5355\u7684\u65b9\u5f0f\uff0c\u4f46\u540c\u6837\u4e0d\u9002\u5408\u590d\u6742\u7684HTML\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">html_doc = &quot;&quot;&quot;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Example Page&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;p&gt;This is a paragraph.&lt;\/p&gt;<\/p>\n<p>    &lt;div&gt;Here is some &lt;p&gt;text with a paragraph tag&lt;\/p&gt; inside a div.&lt;\/div&gt;<\/p>\n<p>    &lt;p&gt;Another paragraph.&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<h2><strong>\u53bb\u6389&lt;p&gt;\u6807\u7b7e<\/strong><\/h2>\n<p>clean_html = html_doc.replace(&#39;&lt;p&gt;&#39;, &#39;&#39;).replace(&#39;&lt;\/p&gt;&#39;, &#39;&#39;)<\/p>\n<p>print(clean_html)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u7b80\u5355\u7684HTML\u6587\u6863\uff0c\u4f46\u5bf9\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\u53ef\u80fd\u4f1a\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406HTML\u6587\u6863\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53bb\u6389<code>&lt;p&gt;<\/code>\u6807\u7b7e\u81f3\u5173\u91cd\u8981\u3002<strong>\u4f7f\u7528BeautifulSoup\u662f\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u51c6\u786e\u89e3\u6790\u548c\u5904\u7406HTML\u6587\u6863\uff0c\u4fdd\u6301\u5185\u5bb9\u7684\u5b8c\u6574\u6027<\/strong>\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5b57\u7b26\u4e32\u66ff\u6362\u65b9\u6cd5\u867d\u7136\u53ef\u4ee5\u5feb\u901f\u89e3\u51b3\u7b80\u5355\u95ee\u9898\uff0c\u4f46\u5728\u5904\u7406\u590d\u6742\u7684HTML\u7ed3\u6784\u65f6\uff0c\u53ef\u80fd\u4f1a\u5e26\u6765\u610f\u60f3\u4e0d\u5230\u7684\u95ee\u9898\u3002\u5728\u9009\u62e9\u65b9\u6cd5\u65f6\uff0c\u5e94\u8be5\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548cHTML\u6587\u6863\u7684\u590d\u6742\u7a0b\u5ea6\u6765\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u79fb\u9664HTML\u6807\u7b7e\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u79fb\u9664HTML\u6807\u7b7e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>BeautifulSoup<\/code>\u5e93\u3002\u8fd9\u4e2a\u5e93\u80fd\u591f\u8f7b\u677e\u5730\u89e3\u6790HTML\u6587\u6863\u5e76\u63d0\u53d6\u6587\u672c\u5185\u5bb9\u3002\u60a8\u53ea\u9700\u5b89\u88c5<code>beautifulsoup4<\/code>\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>get_text()<\/code>\u65b9\u6cd5\u63d0\u53d6\u7eaf\u6587\u672c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">from bs4 import BeautifulSoup\n\nhtml_content = &quot;&lt;p&gt;\u8fd9\u662f\u4e00\u4e2a\u6bb5\u843d\u3002&lt;\/p&gt;&quot;\nsoup = BeautifulSoup(html_content, &quot;html.parser&quot;)\ntext = soup.get_text()\nprint(text)  # \u8f93\u51fa\uff1a\u8fd9\u662f\u4e00\u4e2a\u6bb5\u843d\u3002\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5426\u53ef\u4ee5\u53bb\u6389HTML\u6807\u7b7e\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4e5f\u53ef\u4ee5\u53bb\u6389HTML\u6807\u7b7e\u3002\u5c3d\u7ba1\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982<code>BeautifulSoup<\/code>\u7a33\u5065\uff0c\u4f46\u5728\u7b80\u5355\u60c5\u51b5\u4e0b\u4ecd\u7136\u6709\u6548\u3002\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u7684<code>sub()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\n\nhtml_content = &quot;&lt;p&gt;\u8fd9\u662f\u4e00\u4e2a\u6bb5\u843d\u3002&lt;\/p&gt;&quot;\ntext = re.sub(r&#39;&lt;.*?&gt;&#39;, &#39;&#39;, html_content)\nprint(text)  # \u8f93\u51fa\uff1a\u8fd9\u662f\u4e00\u4e2a\u6bb5\u843d\u3002\n<\/code><\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u590d\u6742HTML\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u95ee\u9898\u3002<\/p>\n<p><strong>\u5728\u5904\u7406HTML\u5185\u5bb9\u65f6\uff0c\u5982\u4f55\u4fdd\u8bc1\u6587\u672c\u7684\u683c\u5f0f\u548c\u5b8c\u6574\u6027\uff1f<\/strong><br \/>\u786e\u4fdd\u6587\u672c\u683c\u5f0f\u548c\u5b8c\u6574\u6027\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>html.parser<\/code>\u6216<code>lxml<\/code>\u7b49\u89e3\u6790\u5668\uff0c\u8fd9\u4e9b\u89e3\u6790\u5668\u80fd\u591f\u6b63\u786e\u5904\u7406\u5d4c\u5957\u548c\u4e0d\u5b8c\u6574\u7684HTML\u6807\u7b7e\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u5728\u63d0\u53d6\u6587\u672c\u540e\uff0c\u4f7f\u7528<code>strip()<\/code>\u65b9\u6cd5\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u683c\u548c\u6362\u884c\uff0c\u4ee5\u786e\u4fdd\u7ed3\u679c\u7684\u6574\u6d01\u6027\u3002\u4f7f\u7528<code>BeautifulSoup<\/code>\u65f6\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u53c2\u6570\u6765\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">text = soup.get_text(separator=&#39; &#39;, strip=True)\nprint(text)  # \u8f93\u51fa\uff1a\u8fd9\u662f\u4e00\u4e2a\u6bb5\u843d\u3002\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u5728\u63d0\u53d6\u6587\u672c\u65f6\u52a0\u5165\u7a7a\u683c\uff0c\u4ece\u800c\u4fdd\u6301\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"&lt;img src=&quot;https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073827\/69c40960-b87d-4 ...","protected":false},"author":3,"featured_media":938278,"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\/938275"}],"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=938275"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/938275\/revisions"}],"predecessor-version":[{"id":938280,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/938275\/revisions\/938280"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/938278"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=938275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=938275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=938275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}