{"id":1108732,"date":"2025-01-08T17:03:08","date_gmt":"2025-01-08T09:03:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108732.html"},"modified":"2025-01-08T17:03:10","modified_gmt":"2025-01-08T09:03:10","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%b0%86tml%e6%96%87%e4%bb%b6%e8%a7%a3%e6%9e%90%e4%b8%ba%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108732.html","title":{"rendered":"\u5982\u4f55\u7528python\u5c06tml\u6587\u4ef6\u89e3\u6790\u4e3a\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072242\/b9e7341f-5a7f-42df-b1a1-86b51b562bd0.webp\" alt=\"\u5982\u4f55\u7528python\u5c06tml\u6587\u4ef6\u89e3\u6790\u4e3a\u5b57\u5178\" \/><\/p>\n<p><p> \u8981\u5c06HTML\u6587\u4ef6\u89e3\u6790\u4e3a\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u4e00\u4e9b\u6d41\u884c\u7684\u5e93\uff0c\u5982BeautifulSoup\u548clxml\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u89e3\u6790\u548c\u5904\u7406HTML\u6570\u636e\u3002<strong>\u9996\u5148\u9700\u8981\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff0c\u7136\u540e\u7f16\u5199\u4ee3\u7801\u6765\u89e3\u6790HTML\u5185\u5bb9\u3001\u63d0\u53d6\u6570\u636e\u5e76\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5177\u4f53\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u4f9d\u8d56\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u4e86BeautifulSoup\u548clxml\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4 lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bfb\u53d6\u548c\u89e3\u6790HTML\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9996\u5148\u8bfb\u53d6HTML\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528BeautifulSoup\u89e3\u6790\u5b83\u3002BeautifulSoup\u652f\u6301\u591a\u79cd\u89e3\u6790\u5668\uff0c\u5176\u4e2dlxml\u662f\u4e00\u4e2a\u975e\u5e38\u9ad8\u6548\u7684\u89e3\u6790\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u8bfb\u53d6HTML\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;example.html&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    html_content = file.read()<\/p>\n<h2><strong>\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML<\/strong><\/h2>\n<p>soup = BeautifulSoup(html_content, &#39;lxml&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5c06HTML\u89e3\u6790\u4e3a\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u6839\u636eHTML\u7684\u7ed3\u6784\u6765\u63d0\u53d6\u7279\u5b9a\u7684\u6570\u636e\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff0c\u5047\u8bbeHTML\u6587\u4ef6\u5305\u542b\u4e00\u4e2a\u8868\u683c\uff0c\u6211\u4eec\u8981\u5c06\u8868\u683c\u6570\u636e\u89e3\u6790\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def parse_table_to_dict(soup):<\/p>\n<p>    table = soup.find(&#39;table&#39;)<\/p>\n<p>    headers = [header.text for header in table.find_all(&#39;th&#39;)]<\/p>\n<p>    rows = table.find_all(&#39;tr&#39;)[1:]  # \u8df3\u8fc7\u8868\u5934\u884c<\/p>\n<p>    data = []<\/p>\n<p>    for row in rows:<\/p>\n<p>        cells = row.find_all(&#39;td&#39;)<\/p>\n<p>        row_data = {headers[i]: cells[i].text for i in range(len(headers))}<\/p>\n<p>        data.append(row_data)<\/p>\n<p>    return data<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u5e76\u6253\u5370\u7ed3\u679c<\/strong><\/h2>\n<p>parsed_data = parse_table_to_dict(soup)<\/p>\n<p>print(parsed_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6df1\u5165\u89e3\u6790\u4e0d\u540cHTML\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u6839\u636eHTML\u6587\u4ef6\u7684\u5177\u4f53\u7ed3\u6784\uff0c\u53ef\u80fd\u9700\u8981\u7f16\u5199\u4e0d\u540c\u7684\u89e3\u6790\u903b\u8f91\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684HTML\u7ed3\u6784\u89e3\u6790\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u89e3\u6790\u5d4c\u5957\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5047\u8bbeHTML\u5305\u542b\u5d4c\u5957\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6765\u89e3\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def parse_list_to_dict(ul):<\/p>\n<p>    result = []<\/p>\n<p>    for li in ul.find_all(&#39;li&#39;, recursive=False):<\/p>\n<p>        item = {}<\/p>\n<p>        if li.find(&#39;ul&#39;):<\/p>\n<p>            item[&#39;text&#39;] = li.contents[0].strip()<\/p>\n<p>            item[&#39;children&#39;] = parse_list_to_dict(li.find(&#39;ul&#39;))<\/p>\n<p>        else:<\/p>\n<p>            item[&#39;text&#39;] = li.text.strip()<\/p>\n<p>        result.append(item)<\/p>\n<p>    return result<\/p>\n<h2><strong>\u627e\u5230\u9876\u5c42\u7684&lt;ul&gt;\u6807\u7b7e\u5e76\u89e3\u6790<\/strong><\/h2>\n<p>ul = soup.find(&#39;ul&#39;)<\/p>\n<p>parsed_list = parse_list_to_dict(ul)<\/p>\n<p>print(parsed_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u89e3\u6790\u5d4c\u5957\u5b57\u5178\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u5982\u679cHTML\u5305\u542b\u5d4c\u5957\u7684\u5b57\u5178\u7ed3\u6784\uff0c\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u89e3\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def parse_div_to_dict(div):<\/p>\n<p>    result = {}<\/p>\n<p>    for child in div.find_all(recursive=False):<\/p>\n<p>        if child.name == &#39;div&#39; and child.get(&#39;id&#39;):<\/p>\n<p>            result[child[&#39;id&#39;]] = parse_div_to_dict(child)<\/p>\n<p>        else:<\/p>\n<p>            result[child.get(&#39;id&#39;, child.name)] = child.text.strip()<\/p>\n<p>    return result<\/p>\n<h2><strong>\u627e\u5230\u6839&lt;div&gt;\u6807\u7b7e\u5e76\u89e3\u6790<\/strong><\/h2>\n<p>root_div = soup.find(&#39;div&#39;, id=&#39;root&#39;)<\/p>\n<p>parsed_div = parse_div_to_dict(root_div)<\/p>\n<p>print(parsed_div)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u7279\u6b8a\u60c5\u51b5\u548c\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cHTML\u6587\u4ef6\u53ef\u80fd\u5305\u542b\u8bb8\u591a\u590d\u6742\u7684\u60c5\u51b5\uff0c\u5982\u5d4c\u5957\u7ed3\u6784\u3001\u52a8\u6001\u5185\u5bb9\u7b49\u3002\u9700\u8981\u7075\u6d3b\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u52a8\u6001\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5305\u542b\u52a8\u6001\u5185\u5bb9\u7684HTML\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u6216\u7c7b\u4f3c\u5de5\u5177\u6765\u6355\u83b7\u5b8c\u6574\u7684\u9875\u9762\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u4f7f\u7528Selenium\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u7684HTML\u5185\u5bb9<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<p>driver.get(&#39;http:\/\/example.com&#39;)<\/p>\n<p>html_content = driver.page_source<\/p>\n<p>driver.quit()<\/p>\n<h2><strong>\u4f7f\u7528BeautifulSoup\u89e3\u6790\u83b7\u53d6\u7684HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(html_content, &#39;lxml&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f18\u5316\u89e3\u6790\u8fc7\u7a0b<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578bHTML\u6587\u4ef6\uff0c\u89e3\u6790\u8fc7\u7a0b\u53ef\u80fd\u4f1a\u6bd4\u8f83\u8017\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f18\u5316\u4ee3\u7801\u548c\u4f7f\u7528\u591a\u7ebf\u7a0b\u7b49\u65b9\u5f0f\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<p>def parse_row(row, headers):<\/p>\n<p>    cells = row.find_all(&#39;td&#39;)<\/p>\n<p>    return {headers[i]: cells[i].text for i in range(len(headers))}<\/p>\n<p>def parse_table_to_dict_optimized(soup):<\/p>\n<p>    table = soup.find(&#39;table&#39;)<\/p>\n<p>    headers = [header.text for header in table.find_all(&#39;th&#39;)]<\/p>\n<p>    rows = table.find_all(&#39;tr&#39;)[1:]<\/p>\n<p>    with concurrent.futures.ThreadPoolExecutor() as executor:<\/p>\n<p>        data = list(executor.map(lambda row: parse_row(row, headers), rows))<\/p>\n<p>    return data<\/p>\n<h2><strong>\u8c03\u7528\u4f18\u5316\u540e\u7684\u51fd\u6570\u5e76\u6253\u5370\u7ed3\u679c<\/strong><\/h2>\n<p>parsed_data_optimized = parse_table_to_dict_optimized(soup)<\/p>\n<p>print(parsed_data_optimized)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u5c06HTML\u6587\u4ef6\u89e3\u6790\u4e3a\u5b57\u5178<\/strong>\u9700\u8981\u6839\u636e\u5177\u4f53\u7684HTML\u7ed3\u6784\u7f16\u5199\u76f8\u5e94\u7684\u89e3\u6790\u903b\u8f91\u3002\u4f7f\u7528BeautifulSoup\u548clxml\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u89e3\u6790\u548c\u5904\u7406HTML\u6570\u636e\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u638c\u63e1\u8fd9\u4e9b\u5de5\u5177\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5c06HTML\u5185\u5bb9\u8f6c\u6362\u4e3a\u5b57\u5178\u683c\u5f0f\uff0c\u65b9\u4fbf\u540e\u7eed\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6tml\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8bfb\u53d6tml\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u7ed3\u5408\u5e93\u5982<code>xml.etree.ElementTree<\/code>\u6216<code>BeautifulSoup<\/code>\u6765\u5904\u7406tml\u683c\u5f0f\u3002\u9996\u5148\uff0c\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\uff0c\u7136\u540e\u5229\u7528\u8fd9\u4e9b\u5e93\u89e3\u6790\u6587\u4ef6\u5185\u5bb9\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u6613\u4e8e\u64cd\u4f5c\u7684\u683c\u5f0f\u3002\u786e\u4fdd\u6587\u4ef6\u7f16\u7801\u6b63\u786e\uff0c\u4ee5\u907f\u514d\u8bfb\u53d6\u9519\u8bef\u3002<\/p>\n<p><strong>\u89e3\u6790tml\u6587\u4ef6\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u5e38\u89c1\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u89e3\u6790tml\u6587\u4ef6\u65f6\uff0c\u5e38\u89c1\u95ee\u9898\u5305\u62ec\u6587\u4ef6\u683c\u5f0f\u4e0d\u6b63\u786e\u3001\u6807\u7b7e\u672a\u95ed\u5408\u3001\u7f16\u7801\u95ee\u9898\u7b49\u3002\u8fd9\u4e9b\u95ee\u9898\u53ef\u80fd\u5bfc\u81f4\u89e3\u6790\u5931\u8d25\u6216\u751f\u6210\u4e0d\u5b8c\u6574\u7684\u5b57\u5178\u3002\u5efa\u8bae\u5728\u89e3\u6790\u4e4b\u524d\u5bf9\u6587\u4ef6\u8fdb\u884c\u9a8c\u8bc1\uff0c\u786e\u4fdd\u5176\u7b26\u5408tml\u7684\u6807\u51c6\u683c\u5f0f\u3002\u6b64\u5916\uff0c\u6355\u83b7\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u9c81\u68d2\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u89e3\u6790\u540e\u7684\u5b57\u5178\u4fdd\u5b58\u4e3a\u5176\u4ed6\u683c\u5f0f\uff0c\u5982JSON\uff1f<\/strong><br \/>\u5c06\u89e3\u6790\u540e\u7684\u5b57\u5178\u4fdd\u5b58\u4e3aJSON\u683c\u5f0f\u975e\u5e38\u7b80\u5355\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b57\u5178\u7ed3\u6784\u662f\u53ef\u4ee5\u5e8f\u5217\u5316\u7684\uff0c\u7136\u540e\u8c03\u7528<code>json.dump()<\/code>\u6216<code>json.dumps()<\/code>\u65b9\u6cd5\u5c06\u5b57\u5178\u5199\u5165\u6587\u4ef6\u6216\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u6837\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u6216\u5171\u4eab\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06HTML\u6587\u4ef6\u89e3\u6790\u4e3a\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u4e00\u4e9b\u6d41\u884c\u7684\u5e93\uff0c\u5982BeautifulSoup\u548clxml\u3002\u8fd9\u4e9b\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1108742,"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\/1108732"}],"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=1108732"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108732\/revisions"}],"predecessor-version":[{"id":1108743,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108732\/revisions\/1108743"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108742"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}