{"id":960126,"date":"2024-12-27T03:46:53","date_gmt":"2024-12-26T19:46:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/960126.html"},"modified":"2024-12-27T03:46:56","modified_gmt":"2024-12-26T19:46:56","slug":"python%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96html%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/960126.html","title":{"rendered":"python\u5982\u4f55\u63d0\u53d6html\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102734\/cb6e1198-a16e-482f-a679-5c2a46ddecae.webp\" alt=\"python\u5982\u4f55\u63d0\u53d6html\u4ee3\u7801\" \/><\/p>\n<p><p> <strong>Python\u63d0\u53d6HTML\u4ee3\u7801\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528BeautifulSoup\u5e93\u3001lxml\u5e93\u3001\u4ee5\u53ca\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/strong>\u5728\u8fd9\u4e09\u79cd\u65b9\u6cd5\u4e2d\uff0c<strong>BeautifulSoup<\/strong>\u662f\u6700\u5e38\u7528\u548c\u6700\u4fbf\u6377\u7684\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u7684API\u6765\u89e3\u6790\u548c\u63d0\u53d6HTML\u548cXML\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528BeautifulSoup\u6765\u63d0\u53d6HTML\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p><strong>BeautifulSoup\u5e93\u7684\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2aPython\u5e93\uff0c\u7528\u4e8e\u4eceHTML\u548cXML\u6587\u4ef6\u4e2d\u63d0\u53d6\u6570\u636e\u3002\u5b83\u901a\u8fc7\u5c06\u8fd9\u4e9b\u6587\u4ef6\u89e3\u6790\u4e3a\u6811\u72b6\u7ed3\u6784\uff0c\u4f7f\u5f97\u63d0\u53d6\u7279\u5b9a\u5143\u7d20\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528BeautifulSoup\u63d0\u53d6HTML\u4ee3\u7801\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5BeautifulSoup\u548c\u76f8\u5173\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528BeautifulSoup\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u8be5\u5e93\u4ee5\u53ca\u7528\u4e8e\u89e3\u6790HTML\u7684\u89e3\u6790\u5668\u3002\u901a\u5e38\uff0clxml\u6216html5lib\u662f\u63a8\u8350\u7684\u89e3\u6790\u5668\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u5b83\u4eec\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<\/li>\n<li>\n<p><strong>\u52a0\u8f7dHTML\u6587\u6863<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684\u5185\u7f6e<code>requests<\/code>\u5e93\u6765\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u5e76\u901a\u8fc7BeautifulSoup\u8fdb\u884c\u89e3\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>url = &#39;http:\/\/example.com&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>soup = BeautifulSoup(response.content, &#39;lxml&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u67e5\u627e\u548c\u63d0\u53d6\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u63d0\u4f9b\u7684\u65b9\u6cd5\u6765\u67e5\u627e\u548c\u63d0\u53d6\u6240\u9700\u7684HTML\u5143\u7d20\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec<code>find_all()<\/code>\u3001<code>find()<\/code>\u3001<code>select()<\/code>\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d0\u53d6\u6240\u6709\u7684\u6bb5\u843d<\/p>\n<p>paragraphs = soup.find_all(&#39;p&#39;)<\/p>\n<p>for p in paragraphs:<\/p>\n<p>    print(p.get_text())<\/p>\n<h2><strong>\u63d0\u53d6\u7279\u5b9a\u7684class<\/strong><\/h2>\n<p>special_div = soup.find(&#39;div&#39;, class_=&#39;special-class&#39;)<\/p>\n<p>print(special_div.get_text())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u63d0\u53d6\u7684\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u63d0\u53d6\u7684\u6570\u636e\u901a\u5e38\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\uff0c\u4f8b\u5982\u6e05\u7406\u6587\u672c\u3001\u4fdd\u5b58\u5230\u6587\u4ef6\u3001\u6216\u8fdb\u884c\u5206\u6790\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u6e05\u7406\u6587\u672c\u4e2d\u7684\u591a\u4f59\u7a7a\u683c<\/strong><\/h2>\n<p>cleaned_text = re.sub(r&#39;\\s+&#39;, &#39; &#39;, special_div.get_text())<\/p>\n<p>print(cleaned_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e94\u7528\u6848\u4f8b<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cBeautifulSoup\u53ef\u4ee5\u7528\u4e8e\u7f51\u7edc\u722c\u866b\u3001\u6570\u636e\u5206\u6790\u548c\u81ea\u52a8\u5316\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u63d0\u53d6\u65b0\u95fb\u7f51\u7ad9\u4e0a\u7684\u6587\u7ae0\u6807\u9898\u548c\u94fe\u63a5\uff0c\u6216\u8005\u5206\u6790\u5728\u7ebf\u5546\u54c1\u7684\u4ef7\u683c\u548c\u8bc4\u8bba\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d0\u53d6\u6240\u6709\u6587\u7ae0\u6807\u9898\u548c\u94fe\u63a5<\/p>\n<p>articles = soup.find_all(&#39;h2&#39;, class_=&#39;article-title&#39;)<\/p>\n<p>for article in articles:<\/p>\n<p>    title = article.get_text()<\/p>\n<p>    link = article.a[&#39;href&#39;]<\/p>\n<p>    print(f&#39;Title: {title}, Link: {link}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u4e8c\u3001lxml\u5e93\u7684\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>lxml\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u7684\u5f3a\u5927\u5e93\uff0c\u4e0eBeautifulSoup\u7c7b\u4f3c\uff0c\u4f46\u5b83\u66f4\u5feb\u4e14\u529f\u80fd\u66f4\u5f3a\u5927\u3002\u4f7f\u7528lxml\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5lxml<\/strong><\/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><strong>\u89e3\u6790HTML\u6587\u6863<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from lxml import etree<\/p>\n<p>parser = etree.HTMLParser()<\/p>\n<p>tree = etree.parse(&#39;http:\/\/example.com&#39;, parser)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63d0\u53d6\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528XPath\u8bed\u6cd5\u6765\u67e5\u627e\u548c\u63d0\u53d6\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d0\u53d6\u6240\u6709\u6bb5\u843d<\/p>\n<p>paragraphs = tree.xpath(&#39;\/\/p&#39;)<\/p>\n<p>for p in paragraphs:<\/p>\n<p>    print(p.text)<\/p>\n<h2><strong>\u63d0\u53d6\u7279\u5b9a\u7684class<\/strong><\/h2>\n<p>special_divs = tree.xpath(&#39;\/\/div[@class=&quot;special-class&quot;]&#39;)<\/p>\n<p>for div in special_divs:<\/p>\n<p>    print(div.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u867d\u7136\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0d\u5982BeautifulSoup\u548clxml\u76f4\u89c2\u548c\u53ef\u9760\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b83\u4eec\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u7684HTML\u63d0\u53d6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>html_content = &#39;&lt;html&gt;...&lt;\/html&gt;&#39;  # \u5047\u8bbe\u8fd9\u662f\u4f60\u7684HTML\u5185\u5bb9<\/p>\n<p>paragraphs = re.findall(r&#39;&lt;p&gt;(.*?)&lt;\/p&gt;&#39;, html_content, re.DOTALL)<\/p>\n<p>for p in paragraphs:<\/p>\n<p>    print(p)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0d\u9002\u5408\u590d\u6742\u7684HTML\u89e3\u6790\uff0c\u56e0\u4e3aHTML\u7684\u5d4c\u5957\u548c\u4e0d\u89c4\u5219\u6027\u53ef\u80fd\u5bfc\u81f4\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u89e3\u6790\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790HTML\u65f6\u9700\u8981\u8c28\u614e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u6765\u63d0\u53d6HTML\u4ee3\u7801\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53d6\u51b3\u4e8e\u4efb\u52a1\u7684\u590d\u6742\u6027\u548c\u5177\u4f53\u9700\u6c42\u3002<strong>BeautifulSoup<\/strong>\u56e0\u5176\u6613\u7528\u6027\u548c\u5f3a\u5927\u7684\u529f\u80fd\u800c\u5e7f\u53d7\u6b22\u8fce\uff0c\u800c<strong>lxml<\/strong>\u5219\u5728\u9700\u8981\u66f4\u9ad8\u6027\u80fd\u548c\u66f4\u590d\u6742\u89e3\u6790\u65f6\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u4efb\u52a1\uff0c<strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u4e5f\u53ef\u4ee5\u662f\u4e00\u79cd\u9009\u62e9\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u5de5\u5177\uff0c\u7406\u89e3HTML\u6587\u6863\u7684\u7ed3\u6784\u548c\u6240\u9700\u6570\u636e\u7684\u7279\u6027\u90fd\u662f\u6210\u529f\u63d0\u53d6\u6570\u636e\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5e93\u63d0\u53d6HTML\u4ee3\u7801\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u5f3a\u5927\u7684\u5e93\u6765\u63d0\u53d6HTML\u4ee3\u7801\uff0c\u4f8b\u5982Beautiful Soup\u548clxml\u3002Beautiful Soup\u662f\u4e00\u79cd\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684\u5e93\uff0c\u5b83\u53ef\u4ee5\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u51fa\u6240\u9700\u7684\u4fe1\u606f\u3002\u4f7f\u7528Beautiful Soup\u65f6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a\u9996\u5148\uff0c\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528Beautiful Soup\u89e3\u6790HTML\u6587\u6863\uff0c\u6700\u540e\u901a\u8fc7\u9009\u62e9\u5668\u67e5\u627e\u6240\u9700\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u63d0\u53d6HTML\u4ee3\u7801\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u548c\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u63d0\u53d6HTML\u4ee3\u7801\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u672a\u6b63\u786e\u5904\u7406\u7f51\u7edc\u8bf7\u6c42\u5bfc\u81f4\u7684\u9519\u8bef\u54cd\u5e94\u3001\u672a\u6b63\u786e\u89e3\u6790HTML\u7ed3\u6784\u5bfc\u81f4\u7684\u6570\u636e\u7f3a\u5931\uff0c\u4ee5\u53ca\u672a\u5904\u7406\u5b57\u7b26\u7f16\u7801\u95ee\u9898\u3002\u5728\u63d0\u53d6\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u5408\u9002\u7684\u8bf7\u6c42\u5934\uff0c\u4ee5\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff1b\u540c\u65f6\uff0c\u6ce8\u610f\u68c0\u67e5HTML\u7684\u7ed3\u6784\u53d8\u5316\uff0c\u56e0\u4e3a\u7f51\u9875\u5185\u5bb9\u53ef\u80fd\u4f1a\u968f\u7740\u65f6\u95f4\u800c\u53d8\u5316\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u63d0\u53d6HTML\u4ee3\u7801\u540e\u7684\u6570\u636e\uff1f<\/strong><br \/>\u63d0\u53d6HTML\u4ee3\u7801\u540e\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u548c\u683c\u5f0f\u5316\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u5206\u6790\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u5c06\u63d0\u53d6\u5230\u7684\u6570\u636e\u5b58\u50a8\u5728DataFrame\u4e2d\uff0c\u65b9\u4fbf\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u53d6\u548c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6570\u636e\u3002\u8fd9\u4e9b\u5904\u7406\u6b65\u9aa4\u5c06\u4f7f\u6570\u636e\u66f4\u5177\u53ef\u7528\u6027\uff0c\u4fbf\u4e8e\u751f\u6210\u62a5\u544a\u6216\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u63d0\u53d6HTML\u4ee3\u7801\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528BeautifulSoup\u5e93\u3001lxml\u5e93\u3001\u4ee5\u53ca\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u5728 [&hellip;]","protected":false},"author":3,"featured_media":960135,"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\/960126"}],"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=960126"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960126\/revisions"}],"predecessor-version":[{"id":960137,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960126\/revisions\/960137"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/960135"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=960126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=960126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=960126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}