{"id":1008740,"date":"2024-12-27T11:05:31","date_gmt":"2024-12-27T03:05:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1008740.html"},"modified":"2024-12-27T11:05:34","modified_gmt":"2024-12-27T03:05:34","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e6%9c%ac%e5%9c%b0html","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1008740.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6\u672c\u5730html"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083931\/9511cd6e-effe-4c6b-871b-85c619760a4c.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6\u672c\u5730html\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6\u672c\u5730HTML\u6587\u4ef6\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u3001BeautifulSoup\u5e93\u89e3\u6790\u3001\u4ee5\u53ca\u4f7f\u7528pandas\u5e93\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u63a8\u8350\u4f7f\u7528BeautifulSoup\u5e93\u8fdb\u884cHTML\u89e3\u6790\uff0c\u56e0\u4e3a\u5176\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8be6\u7ec6\u8bf4\u660e\u4f7f\u7528BeautifulSoup\u5e93\u7684\u65b9\u6cd5\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u7b80\u5355\u4ecb\u7ecd\u5176\u4ed6\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570<\/strong>\uff1a\u8fd9\u662f\u6700\u7b80\u5355\u7684\u8bfb\u53d6\u65b9\u6cd5\uff0c\u53ef\u4ee5\u76f4\u63a5\u8bfb\u53d6HTML\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u4f46\u4e0d\u89e3\u6790HTML\u7ed3\u6784\u3002<\/li>\n<li><strong>\u4f7f\u7528pandas\u5e93\u8bfb\u53d6HTML\u8868\u683c<\/strong>\uff1a\u5982\u679c\u4f60\u7684HTML\u6587\u4ef6\u4e2d\u5305\u542b\u8868\u683c\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u7684<code>read_html()<\/code>\u51fd\u6570\u76f4\u63a5\u63d0\u53d6\u8868\u683c\u5185\u5bb9\u3002<\/li>\n<li><strong>\u4f7f\u7528BeautifulSoup\u5e93\u89e3\u6790HTML<\/strong>\uff1a\u8fd9\u662f\u5904\u7406HTML\u6587\u4ef6\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u89e3\u6790\u548c\u63d0\u53d6HTML\u6587\u6863\u4e2d\u7684\u6570\u636e\u3002<\/li>\n<\/ol>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528BeautifulSoup\u5e93\u6765\u8bfb\u53d6\u548c\u89e3\u6790\u672c\u5730HTML\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5BeautifulSoup\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86BeautifulSoup\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\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><p>BeautifulSoup\u5e93\u4f9d\u8d56\u4e8e\u89e3\u6790\u5668\u6765\u89e3\u6790HTML\u6587\u4ef6\uff0c<code>lxml<\/code>\u662f\u5176\u4e2d\u4e00\u4e2a\u5e38\u7528\u7684\u89e3\u6790\u5668\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u6587\u4ef6<\/h3>\n<\/p>\n<ol>\n<li><strong>\u8bfb\u53d6HTML\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528Python\u7684\u5185\u7f6e<code>open()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6HTML\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u5047\u8bbe\u4f60\u7684HTML\u6587\u4ef6\u540d\u4e3a<code>example.html<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.html&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;) as file:<\/p>\n<p>    html_content = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5c06HTML\u6587\u4ef6\u7684\u5185\u5bb9\u8bfb\u53d6\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/li>\n<\/ol>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528BeautifulSoup\u6765\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, &quot;lxml&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u4f7f\u7528\u4e86<code>lxml<\/code>\u4f5c\u4e3a\u89e3\u6790\u5668\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>html.parser<\/code>\u4f5c\u4e3a\u66ff\u4ee3\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u63d0\u53d6HTML\u6570\u636e<\/strong><\/li>\n<\/ol>\n<p><p>BeautifulSoup\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u63d0\u53d6HTML\u6587\u6863\u4e2d\u7684\u6570\u636e\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u83b7\u53d6\u6807\u7b7e\u5185\u5bb9<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u6807\u7b7e\u540d\u76f4\u63a5\u83b7\u53d6\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u6240\u6709<code>&lt;p&gt;<\/code>\u6807\u7b7e\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">paragraphs = soup.find_all(&quot;p&quot;)<\/p>\n<p>for p in paragraphs:<\/p>\n<p>    print(p.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u7279\u5b9a\u5c5e\u6027\u7684\u6807\u7b7e<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u5c5e\u6027\u540d\u83b7\u53d6\u7279\u5b9a\u6807\u7b7e\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u6240\u6709\u7c7b\u540d\u4e3a<code>&quot;example-class&quot;<\/code>\u7684<code>&lt;div&gt;<\/code>\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">divs = soup.find_all(&quot;div&quot;, class_=&quot;example-class&quot;)<\/p>\n<p>for div in divs:<\/p>\n<p>    print(div.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u7279\u5b9aID\u7684\u6807\u7b7e<\/strong>\uff1a\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>find()<\/code>\u65b9\u6cd5\u83b7\u53d6\u7279\u5b9aID\u7684\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">specific_div = soup.find(&quot;div&quot;, id=&quot;specific-id&quot;)<\/p>\n<p>print(specific_div.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528pandas\u5e93\u8bfb\u53d6HTML\u8868\u683c<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684HTML\u6587\u4ef6\u4e2d\u5305\u542b\u8868\u683c\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u7684<code>read_html()<\/code>\u51fd\u6570\u76f4\u63a5\u8bfb\u53d6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>tables = pd.read_html(&quot;example.html&quot;)<\/p>\n<p>for table in tables:<\/p>\n<p>    print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>read_html()<\/code>\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542bDataFrame\u7684\u5217\u8868\uff0c\u6bcf\u4e2aDataFrame\u5bf9\u5e94HTML\u6587\u4ef6\u4e2d\u7684\u4e00\u4e2a\u8868\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u89e3\u6790\u540e\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u89e3\u6790HTML\u6587\u4ef6\u540e\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u63d0\u53d6\u7684\u6570\u636e\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6570\u636e\u6e05\u6d17<\/strong>\uff1a\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u683c\u3001\u7279\u6b8a\u5b57\u7b26\u7b49\u3002<\/li>\n<li><strong>\u6570\u636e\u8f6c\u6362<\/strong>\uff1a\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u7279\u5b9a\u683c\u5f0f\uff0c\u4f8b\u5982\u65e5\u671f\u3001\u6570\u5b57\u7b49\u3002<\/li>\n<li><strong>\u6570\u636e\u5206\u6790<\/strong>\uff1a\u5bf9\u63d0\u53d6\u7684\u6570\u636e\u8fdb\u884c\u7edf\u8ba1\u5206\u6790\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u8bfb\u53d6\u548c\u89e3\u6790\u672c\u5730HTML\u6587\u4ef6\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7f51\u9875\u6570\u636e\u63d0\u53d6<\/strong>\uff1a\u53ef\u4ee5\u4ece\u672c\u5730\u4fdd\u5b58\u7684\u7f51\u9875\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002<\/li>\n<li><strong>\u81ea\u52a8\u5316\u6d4b\u8bd5<\/strong>\uff1a\u53ef\u4ee5\u7528\u6765\u89e3\u6790\u6d4b\u8bd5\u62a5\u544a\u7b49HTML\u683c\u5f0f\u7684\u6587\u4ef6\u3002<\/li>\n<li><strong>\u6570\u636e\u5206\u6790<\/strong>\uff1a\u53ef\u4ee5\u4eceHTML\u8868\u683c\u4e2d\u63d0\u53d6\u6570\u636e\u8fdb\u884c\u5206\u6790\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u65b9\u4fbf\u5730\u4f7f\u7528Python\u8bfb\u53d6\u548c\u89e3\u6790\u672c\u5730HTML\u6587\u4ef6\uff0c\u5e76\u6839\u636e\u9700\u8981\u63d0\u53d6\u548c\u5904\u7406\u6570\u636e\u3002\u8fd9\u79cd\u6280\u80fd\u5728\u6570\u636e\u5206\u6790\u3001\u81ea\u52a8\u5316\u6d4b\u8bd5\u7b49\u9886\u57df\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u4ef7\u503c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6\u672c\u5730HTML\u6587\u4ef6\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8bfb\u53d6\u672c\u5730HTML\u6587\u4ef6\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4f60\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\u5e76\u4ee5\u6587\u672c\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>read()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;path\/to\/your\/file.html&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:\n    html_content = file.read()\n    print(html_content)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5c06<code>path\/to\/your\/file.html<\/code>\u66ff\u6362\u4e3a\u4f60\u672c\u5730\u6587\u4ef6\u7684\u5b9e\u9645\u8def\u5f84\u3002<\/p>\n<p><strong>\u5728\u8bfb\u53d6HTML\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7f16\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u8bfb\u53d6HTML\u6587\u4ef6\u65f6\uff0c\u7f16\u7801\u662f\u4e00\u4e2a\u91cd\u8981\u8003\u8651\u56e0\u7d20\u3002\u901a\u5e38\uff0cHTML\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801\u3002\u5728\u6253\u5f00\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u6307\u5b9a<code>encoding=&#39;utf-8&#39;<\/code>\u4ee5\u907f\u514d\u8bfb\u53d6\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u4e71\u7801\u3002\u5982\u679c\u4f60\u4e0d\u786e\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>chardet<\/code>\u5e93\u6765\u68c0\u6d4b\u6587\u4ef6\u7f16\u7801\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u89e3\u6790HTML\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u89e3\u6790HTML\u6587\u4ef6\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528<code>BeautifulSoup<\/code>\u5e93\u6216<code>lxml<\/code>\u5e93\u3002<code>BeautifulSoup<\/code>\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684API\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d0\u53d6\u548c\u5904\u7406HTML\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>BeautifulSoup<\/code>\u89e3\u6790HTML\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">from bs4 import BeautifulSoup\n\nwith open(&#39;path\/to\/your\/file.html&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:\n    html_content = file.read()\n    soup = BeautifulSoup(html_content, &#39;html.parser&#39;)\n    # \u4f8b\u5982\uff0c\u63d0\u53d6\u6240\u6709\u94fe\u63a5\n    for link in soup.find_all(&#39;a&#39;):\n        print(link.get(&#39;href&#39;))\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6HTML\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u548c\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6\u672c\u5730HTML\u6587\u4ef6\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684open()\u51fd\u6570\u3001BeautifulSoup\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1008754,"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\/1008740"}],"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=1008740"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008740\/revisions"}],"predecessor-version":[{"id":1008760,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008740\/revisions\/1008760"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1008754"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1008740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1008740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1008740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}