{"id":1156377,"date":"2025-01-13T18:15:17","date_gmt":"2025-01-13T10:15:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1156377.html"},"modified":"2025-01-13T18:15:20","modified_gmt":"2025-01-13T10:15:20","slug":"python%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85xml%e6%96%87%e4%bb%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1156377.html","title":{"rendered":"python\u5982\u4f55\u5b89\u88c5xml\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25194835\/bfaebb2a-a7c6-4e12-93d4-ce45500b2542.webp\" alt=\"python\u5982\u4f55\u5b89\u88c5xml\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u5b89\u88c5XML\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528\u5185\u7f6e\u5e93\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93lxml\u3001\u4f7f\u7528BeautifulSoup\u3002<\/strong> \u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c<strong>\u4f7f\u7528\u5185\u7f6e\u5e93\u662f\u6700\u57fa\u672c\u7684\u65b9\u5f0f<\/strong>\uff0c\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u6765\u5b89\u88c5\u548c\u89e3\u6790XML\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Python\u5185\u7f6e\u7684xml\u5e93<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684<code>xml.etree.ElementTree<\/code>\u5e93\u662f\u5904\u7406XML\u6587\u4ef6\u7684\u6807\u51c6\u5e93\u3002\u8fd9\u4e2a\u5e93\u63d0\u4f9b\u4e86\u4e00\u7ec4\u7b80\u4fbf\u7684\u65b9\u6cd5\u6765\u89e3\u6790\u548c\u521b\u5efaXML\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u89e3\u6790XML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u89e3\u6790XML\u6587\u4ef6\u662f\u8bfb\u53d6\u6587\u4ef6\u5e76\u5c06\u5176\u5185\u5bb9\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u7684\u8fc7\u7a0b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>ElementTree<\/code>\u5e93\u6765\u89e3\u6790XML\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<h2><strong>\u8bfb\u53d6XML\u6587\u4ef6<\/strong><\/h2>\n<p>tree = ET.parse(&#39;example.xml&#39;)<\/p>\n<p>root = tree.getroot()<\/p>\n<h2><strong>\u8f93\u51fa\u6839\u5143\u7d20\u7684\u6807\u7b7e\u548c\u5c5e\u6027<\/strong><\/h2>\n<p>print(root.tag, root.attrib)<\/p>\n<h2><strong>\u904d\u5386\u6240\u6709\u5b50\u5143\u7d20<\/strong><\/h2>\n<p>for child in root:<\/p>\n<p>    print(child.tag, child.attrib)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>ET.parse(&#39;example.xml&#39;)<\/code>\u8bfb\u53d6\u5e76\u89e3\u6790\u4e86\u4e00\u4e2a\u540d\u4e3a<code>example.xml<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u5b58\u50a8\u5728<code>tree<\/code>\u5bf9\u8c61\u4e2d\u3002<code>tree.getroot()<\/code>\u65b9\u6cd5\u8fd4\u56deXML\u6587\u4ef6\u7684\u6839\u5143\u7d20\uff0c<code>root<\/code>\u5bf9\u8c61\u8868\u793a\u8fd9\u4e2a\u6839\u5143\u7d20\u3002\u901a\u8fc7\u904d\u5386<code>root<\/code>\u7684\u5b50\u5143\u7d20\uff0c\u53ef\u4ee5\u8bbf\u95eeXML\u6587\u4ef6\u7684\u6240\u6709\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u521b\u5efa\u548c\u5199\u5165XML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u548c\u5199\u5165XML\u6587\u4ef6\u662f\u5c06\u6570\u636e\u4ecePython\u5bf9\u8c61\u8f6c\u6362\u4e3aXML\u683c\u5f0f\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u7684\u8fc7\u7a0b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>ElementTree<\/code>\u5e93\u6765\u521b\u5efa\u548c\u5199\u5165XML\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<h2><strong>\u521b\u5efa\u6839\u5143\u7d20<\/strong><\/h2>\n<p>root = ET.Element(&#39;root&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u5b50\u5143\u7d20\u5e76\u6dfb\u52a0\u5230\u6839\u5143\u7d20\u4e2d<\/strong><\/h2>\n<p>child1 = ET.SubElement(root, &#39;child1&#39;)<\/p>\n<p>child1.text = &#39;This is child 1&#39;<\/p>\n<p>child2 = ET.SubElement(root, &#39;child2&#39;)<\/p>\n<p>child2.text = &#39;This is child 2&#39;<\/p>\n<h2><strong>\u521b\u5efaElementTree\u5bf9\u8c61\u5e76\u5199\u5165XML\u6587\u4ef6<\/strong><\/h2>\n<p>tree = ET.ElementTree(root)<\/p>\n<p>tree.write(&#39;output.xml&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>ET.Element(&#39;root&#39;)<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>root<\/code>\u7684\u6839\u5143\u7d20\u3002<code>ET.SubElement(root, &#39;child1&#39;)<\/code>\u548c<code>ET.SubElement(root, &#39;child2&#39;)<\/code>\u65b9\u6cd5\u5206\u522b\u521b\u5efa\u4e86\u4e24\u4e2a\u5b50\u5143\u7d20<code>child1<\/code>\u548c<code>child2<\/code>\uff0c\u5e76\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230\u6839\u5143\u7d20\u4e2d\u3002\u6700\u540e\uff0c<code>ET.ElementTree(root)<\/code>\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a<code>ElementTree<\/code>\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>tree.write(&#39;output.xml&#39;)<\/code>\u65b9\u6cd5\u5c06XML\u6587\u4ef6\u5199\u5165\u78c1\u76d8\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93lxml<\/h3>\n<\/p>\n<p><p><code>lxml<\/code>\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u9ad8\u6548\u7684\u5e93\uff0c\u7528\u4e8e\u5904\u7406XML\u548cHTML\u3002\u5b83\u5177\u6709\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u6027\u80fd\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u5927\u578bXML\u6587\u4ef6\u6216\u6267\u884c\u590d\u6742\u64cd\u4f5c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5b89\u88c5lxml\u5e93<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528<code>lxml<\/code>\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7<code>pip<\/code>\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u89e3\u6790XML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>lxml<\/code>\u5e93\u89e3\u6790XML\u6587\u4ef6\u4e0e\u4f7f\u7528<code>ElementTree<\/code>\u5e93\u7c7b\u4f3c\uff0c\u4f46\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u66f4\u9ad8\u7684\u6027\u80fd\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>lxml<\/code>\u5e93\u6765\u89e3\u6790XML\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from lxml import etree<\/p>\n<h2><strong>\u8bfb\u53d6XML\u6587\u4ef6<\/strong><\/h2>\n<p>tree = etree.parse(&#39;example.xml&#39;)<\/p>\n<p>root = tree.getroot()<\/p>\n<h2><strong>\u8f93\u51fa\u6839\u5143\u7d20\u7684\u6807\u7b7e\u548c\u5c5e\u6027<\/strong><\/h2>\n<p>print(root.tag, root.attrib)<\/p>\n<h2><strong>\u904d\u5386\u6240\u6709\u5b50\u5143\u7d20<\/strong><\/h2>\n<p>for child in root:<\/p>\n<p>    print(child.tag, child.attrib)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>etree.parse(&#39;example.xml&#39;)<\/code>\u8bfb\u53d6\u5e76\u89e3\u6790\u4e86\u4e00\u4e2a\u540d\u4e3a<code>example.xml<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u5b58\u50a8\u5728<code>tree<\/code>\u5bf9\u8c61\u4e2d\u3002<code>tree.getroot()<\/code>\u65b9\u6cd5\u8fd4\u56deXML\u6587\u4ef6\u7684\u6839\u5143\u7d20\uff0c<code>root<\/code>\u5bf9\u8c61\u8868\u793a\u8fd9\u4e2a\u6839\u5143\u7d20\u3002\u901a\u8fc7\u904d\u5386<code>root<\/code>\u7684\u5b50\u5143\u7d20\uff0c\u53ef\u4ee5\u8bbf\u95eeXML\u6587\u4ef6\u7684\u6240\u6709\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2.3 \u521b\u5efa\u548c\u5199\u5165XML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>lxml<\/code>\u5e93\u521b\u5efa\u548c\u5199\u5165XML\u6587\u4ef6\u4e0e\u4f7f\u7528<code>ElementTree<\/code>\u5e93\u7c7b\u4f3c\uff0c\u4f46\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u66f4\u9ad8\u7684\u6027\u80fd\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>lxml<\/code>\u5e93\u6765\u521b\u5efa\u548c\u5199\u5165XML\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from lxml import etree<\/p>\n<h2><strong>\u521b\u5efa\u6839\u5143\u7d20<\/strong><\/h2>\n<p>root = etree.Element(&#39;root&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u5b50\u5143\u7d20\u5e76\u6dfb\u52a0\u5230\u6839\u5143\u7d20\u4e2d<\/strong><\/h2>\n<p>child1 = etree.SubElement(root, &#39;child1&#39;)<\/p>\n<p>child1.text = &#39;This is child 1&#39;<\/p>\n<p>child2 = etree.SubElement(root, &#39;child2&#39;)<\/p>\n<p>child2.text = &#39;This is child 2&#39;<\/p>\n<h2><strong>\u521b\u5efaElementTree\u5bf9\u8c61\u5e76\u5199\u5165XML\u6587\u4ef6<\/strong><\/h2>\n<p>tree = etree.ElementTree(root)<\/p>\n<p>tree.write(&#39;output.xml&#39;, pretty_print=True, xml_declaration=True, encoding=&#39;UTF-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>etree.Element(&#39;root&#39;)<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>root<\/code>\u7684\u6839\u5143\u7d20\u3002<code>etree.SubElement(root, &#39;child1&#39;)<\/code>\u548c<code>etree.SubElement(root, &#39;child2&#39;)<\/code>\u65b9\u6cd5\u5206\u522b\u521b\u5efa\u4e86\u4e24\u4e2a\u5b50\u5143\u7d20<code>child1<\/code>\u548c<code>child2<\/code>\uff0c\u5e76\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230\u6839\u5143\u7d20\u4e2d\u3002\u6700\u540e\uff0c<code>etree.ElementTree(root)<\/code>\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a<code>ElementTree<\/code>\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>tree.write(&#39;output.xml&#39;, pretty_print=True, xml_declaration=True, encoding=&#39;UTF-8&#39;)<\/code>\u65b9\u6cd5\u5c06XML\u6587\u4ef6\u5199\u5165\u78c1\u76d8\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528BeautifulSoup<\/h3>\n<\/p>\n<p><p><code>BeautifulSoup<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u4ef6\u7684\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u4e0d\u5b8c\u6574\u6216\u4e0d\u89c4\u8303\u7684\u6587\u4ef6\u3002\u5b83\u63d0\u4f9b\u4e86\u7b80\u4fbf\u7684API\uff0c\u9002\u5408\u5feb\u901f\u89e3\u6790\u548c\u4fee\u6539XML\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b89\u88c5BeautifulSoup\u5e93<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528<code>BeautifulSoup<\/code>\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7<code>pip<\/code>\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u89e3\u6790XML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>BeautifulSoup<\/code>\u5e93\u89e3\u6790XML\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>BeautifulSoup<\/code>\u5e93\u6765\u89e3\u6790XML\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u8bfb\u53d6XML\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;example.xml&#39;, &#39;r&#39;) as file:<\/p>\n<p>    xml_content = file.read()<\/p>\n<h2><strong>\u89e3\u6790XML\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(xml_content, &#39;xml&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u6839\u5143\u7d20\u7684\u540d\u79f0<\/strong><\/h2>\n<p>print(soup.root.name)<\/p>\n<h2><strong>\u904d\u5386\u6240\u6709\u5b50\u5143\u7d20<\/strong><\/h2>\n<p>for child in soup.root.children:<\/p>\n<p>    if child.name is not None:<\/p>\n<p>        print(child.name, child.attrs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>BeautifulSoup(xml_content, &#39;xml&#39;)<\/code>\u89e3\u6790\u4e86XML\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728<code>soup<\/code>\u5bf9\u8c61\u4e2d\u3002<code>soup.root<\/code>\u8868\u793aXML\u6587\u4ef6\u7684\u6839\u5143\u7d20\uff0c\u901a\u8fc7\u904d\u5386<code>soup.root.children<\/code>\uff0c\u53ef\u4ee5\u8bbf\u95eeXML\u6587\u4ef6\u7684\u6240\u6709\u5b50\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>3.3 \u521b\u5efa\u548c\u5199\u5165XML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>BeautifulSoup<\/code>\u5e93\u521b\u5efa\u548c\u5199\u5165XML\u6587\u4ef6\u540c\u6837\u975e\u5e38\u7b80\u5355\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>BeautifulSoup<\/code>\u5e93\u6765\u521b\u5efa\u548c\u5199\u5165XML\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u521b\u5efa\u6839\u5143\u7d20<\/strong><\/h2>\n<p>soup = BeautifulSoup(&#39;&lt;root&gt;&lt;\/root&gt;&#39;, &#39;xml&#39;)<\/p>\n<p>root = soup.root<\/p>\n<h2><strong>\u521b\u5efa\u5b50\u5143\u7d20\u5e76\u6dfb\u52a0\u5230\u6839\u5143\u7d20\u4e2d<\/strong><\/h2>\n<p>child1 = soup.new_tag(&#39;child1&#39;)<\/p>\n<p>child1.string = &#39;This is child 1&#39;<\/p>\n<p>root.append(child1)<\/p>\n<p>child2 = soup.new_tag(&#39;child2&#39;)<\/p>\n<p>child2.string = &#39;This is child 2&#39;<\/p>\n<p>root.append(child2)<\/p>\n<h2><strong>\u5c06XML\u5185\u5bb9\u5199\u5165\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.xml&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(str(soup))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>BeautifulSoup(&#39;&lt;root&gt;&lt;\/root&gt;&#39;, &#39;xml&#39;)<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u6839\u5143\u7d20\u7684<code>soup<\/code>\u5bf9\u8c61\u3002<code>soup.new_tag(&#39;child1&#39;)<\/code>\u548c<code>soup.new_tag(&#39;child2&#39;)<\/code>\u65b9\u6cd5\u5206\u522b\u521b\u5efa\u4e86\u4e24\u4e2a\u5b50\u5143\u7d20<code>child1<\/code>\u548c<code>child2<\/code>\uff0c\u5e76\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230\u6839\u5143\u7d20\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>file.write(str(soup))<\/code>\u65b9\u6cd5\u5c06XML\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528Python\u5185\u7f6e\u7684xml\u5e93<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u7b80\u5355\u7684XML\u6587\u4ef6\uff0c\u63d0\u4f9b\u57fa\u672c\u7684\u89e3\u6790\u548c\u521b\u5efa\u529f\u80fd\u3002<\/li>\n<li><strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93lxml<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u5927\u578bXML\u6587\u4ef6\u6216\u9700\u8981\u9ad8\u7ea7\u529f\u80fd\u7684\u60c5\u51b5\uff0c\u63d0\u4f9b\u66f4\u9ad8\u7684\u6027\u80fd\u548c\u66f4\u591a\u7684\u529f\u80fd\u3002<\/li>\n<li><strong>\u4f7f\u7528BeautifulSoup<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u4e0d\u5b8c\u6574\u6216\u4e0d\u89c4\u8303\u7684XML\u6587\u4ef6\uff0c\u63d0\u4f9b\u7b80\u4fbf\u7684API\u548c\u5f3a\u5927\u7684\u89e3\u6790\u529f\u80fd\u3002<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u793a\u4f8b\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>5.1 \u8bfb\u53d6\u5e76\u89e3\u6790\u590d\u6742XML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cXML\u6587\u4ef6\u53ef\u80fd\u5305\u542b\u590d\u6742\u7684\u7ed3\u6784\u548c\u5d4c\u5957\u5143\u7d20\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>lxml<\/code>\u5e93\u6765\u89e3\u6790\u590d\u6742\u7684XML\u6587\u4ef6\uff0c\u5e76\u63d0\u53d6\u7279\u5b9a\u7684\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from lxml import etree<\/p>\n<h2><strong>\u8bfb\u53d6\u5e76\u89e3\u6790XML\u6587\u4ef6<\/strong><\/h2>\n<p>tree = etree.parse(&#39;complex_example.xml&#39;)<\/p>\n<p>root = tree.getroot()<\/p>\n<h2><strong>\u63d0\u53d6\u7279\u5b9a\u4fe1\u606f<\/strong><\/h2>\n<p>for element in root.findall(&#39;.\/\/target_element&#39;):<\/p>\n<p>    print(element.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>tree.findall(&#39;.\/\/target_element&#39;)<\/code>\u65b9\u6cd5\u4f7f\u7528XPath\u8868\u8fbe\u5f0f\u67e5\u627e\u6240\u6709\u540d\u4e3a<code>target_element<\/code>\u7684\u5143\u7d20\uff0c\u5e76\u63d0\u53d6\u5176\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>5.2 \u4fee\u6539\u5e76\u4fdd\u5b58XML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4fee\u6539\u73b0\u6709\u7684XML\u6587\u4ef6\u5e76\u4fdd\u5b58\u66f4\u6539\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>lxml<\/code>\u5e93\u6765\u4fee\u6539XML\u6587\u4ef6\uff0c\u5e76\u5c06\u66f4\u6539\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from lxml import etree<\/p>\n<h2><strong>\u8bfb\u53d6\u5e76\u89e3\u6790XML\u6587\u4ef6<\/strong><\/h2>\n<p>tree = etree.parse(&#39;example.xml&#39;)<\/p>\n<p>root = tree.getroot()<\/p>\n<h2><strong>\u4fee\u6539\u5143\u7d20\u5185\u5bb9<\/strong><\/h2>\n<p>for element in root.findall(&#39;.\/\/target_element&#39;):<\/p>\n<p>    element.text = &#39;New content&#39;<\/p>\n<h2><strong>\u4fdd\u5b58\u66f4\u6539\u5230\u6587\u4ef6<\/strong><\/h2>\n<p>tree.write(&#39;modified_example.xml&#39;, pretty_print=True, xml_declaration=True, encoding=&#39;UTF-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>element.text = &#39;New content&#39;<\/code>\u65b9\u6cd5\u4fee\u6539\u4e86\u540d\u4e3a<code>target_element<\/code>\u7684\u5143\u7d20\u7684\u6587\u672c\u5185\u5bb9\uff0c\u5e76\u901a\u8fc7<code>tree.write(&#39;modified_example.xml&#39;, pretty_print=True, xml_declaration=True, encoding=&#39;UTF-8&#39;)<\/code>\u65b9\u6cd5\u5c06\u66f4\u6539\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406XML\u4e2d\u7684\u547d\u540d\u7a7a\u95f4<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406XML\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5e26\u6709\u547d\u540d\u7a7a\u95f4\u7684XML\u3002\u547d\u540d\u7a7a\u95f4\u7528\u4e8e\u533a\u5206\u4e0d\u540c\u7684XML\u5143\u7d20\uff0c\u907f\u514d\u540d\u79f0\u51b2\u7a81\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>lxml<\/code>\u5e93\u6765\u5904\u7406\u5e26\u6709\u547d\u540d\u7a7a\u95f4\u7684XML\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from lxml import etree<\/p>\n<h2><strong>\u8bfb\u53d6\u5e76\u89e3\u6790\u5e26\u6709\u547d\u540d\u7a7a\u95f4\u7684XML\u6587\u4ef6<\/strong><\/h2>\n<p>tree = etree.parse(&#39;namespace_example.xml&#39;)<\/p>\n<p>root = tree.getroot()<\/p>\n<h2><strong>\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4<\/strong><\/h2>\n<p>namespaces = {&#39;ns&#39;: &#39;http:\/\/example.com\/ns&#39;}<\/p>\n<h2><strong>\u67e5\u627e\u5e26\u6709\u547d\u540d\u7a7a\u95f4\u7684\u5143\u7d20<\/strong><\/h2>\n<p>for element in root.findall(&#39;.\/\/ns:target_element&#39;, namespaces):<\/p>\n<p>    print(element.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>namespaces = {&#39;ns&#39;: &#39;http:\/\/example.com\/ns&#39;}<\/code>\u5b9a\u4e49\u4e86\u547d\u540d\u7a7a\u95f4\uff0c\u5e76\u901a\u8fc7<code>root.findall(&#39;.\/\/ns:target_element&#39;, namespaces)<\/code>\u65b9\u6cd5\u67e5\u627e\u5e26\u6709\u547d\u540d\u7a7a\u95f4\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u60a8\u5e94\u8be5\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u4f7f\u7528Python\u6765\u5b89\u88c5\u548c\u89e3\u6790XML\u6587\u4ef6\u3002<strong>\u4f7f\u7528Python\u5185\u7f6e\u7684xml\u5e93\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93lxml\u3001\u4f7f\u7528BeautifulSoup<\/strong>\u662f\u5904\u7406XML\u6587\u4ef6\u7684\u4e09\u79cd\u5e38\u89c1\u65b9\u6cd5\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548cXML\u6587\u4ef6\u7684\u590d\u6742\u7a0b\u5ea6\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528Python\u5185\u7f6e\u7684xml\u5e93<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u7b80\u5355\u7684XML\u6587\u4ef6\uff0c\u63d0\u4f9b\u57fa\u672c\u7684\u89e3\u6790\u548c\u521b\u5efa\u529f\u80fd\u3002<\/li>\n<li><strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93lxml<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u5927\u578bXML\u6587\u4ef6\u6216\u9700\u8981\u9ad8\u7ea7\u529f\u80fd\u7684\u60c5\u51b5\uff0c\u63d0\u4f9b\u66f4\u9ad8\u7684\u6027\u80fd\u548c\u66f4\u591a\u7684\u529f\u80fd\u3002<\/li>\n<li><strong>\u4f7f\u7528BeautifulSoup<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u4e0d\u5b8c\u6574\u6216\u4e0d\u89c4\u8303\u7684XML\u6587\u4ef6\uff0c\u63d0\u4f9b\u7b80\u4fbf\u7684API\u548c\u5f3a\u5927\u7684\u89e3\u6790\u529f\u80fd\u3002<\/li>\n<\/ol>\n<p><p>\u6b64\u5916\uff0c\u8fd8\u4ecb\u7ecd\u4e86\u5982\u4f55\u5904\u7406\u590d\u6742\u7684XML\u6587\u4ef6\u3001\u4fee\u6539\u5e76\u4fdd\u5b58XML\u6587\u4ef6\u3001\u4ee5\u53ca\u5904\u7406\u5e26\u6709\u547d\u540d\u7a7a\u95f4\u7684XML\u6587\u4ef6\u3002\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\uff0c\u60a8\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u5404\u79cdXML\u6587\u4ef6\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406XML\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5904\u7406XML\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>xml<\/code>\u5e93\uff0c\u8be5\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u5904\u7406XML\u6587\u4ef6\u7684\u65b9\u6cd5\u3002\u5e38\u7528\u7684\u6a21\u5757\u5305\u62ec<code>xml.etree.ElementTree<\/code>\u548c<code>xml.dom.minidom<\/code>\u3002\u901a\u8fc7\u8fd9\u4e9b\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u89e3\u6790\u3001\u521b\u5efa\u548c\u4fee\u6539XML\u6587\u4ef6\u3002\u53ea\u9700\u5bfc\u5165\u76f8\u5e94\u7684\u6a21\u5757\u5e76\u4f7f\u7528\u5176\u63d0\u4f9b\u7684\u51fd\u6570\u5373\u53ef\u8f7b\u677e\u5730\u8bfb\u53d6\u548c\u5199\u5165XML\u6570\u636e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5904\u7406XML\u65f6\uff0c\u5e38\u7528\u7684\u5e93\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u9664\u4e86\u5185\u7f6e\u7684<code>xml<\/code>\u5e93\u5916\uff0cPython\u793e\u533a\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u4e00\u4e9b\u6d41\u884c\u7684\u5e93\u6765\u5904\u7406XML\u6587\u4ef6\uff0c\u6bd4\u5982<code>lxml<\/code>\u548c<code>BeautifulSoup<\/code>\u3002<code>lxml<\/code>\u5e93\u4ee5\u5176\u9ad8\u6027\u80fd\u548c\u4e30\u5bcc\u7684\u529f\u80fd\u8457\u79f0\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684XML\u6587\u6863\u3002<code>BeautifulSoup<\/code>\u5219\u66f4\u9002\u5408\u8fdb\u884c\u7f51\u9875\u89e3\u6790\uff0c\u4f46\u4e5f\u652f\u6301XML\u5904\u7406\u3002\u6839\u636e\u60a8\u7684\u5177\u4f53\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<p><strong>\u5982\u4f55\u89e3\u51b3Python\u5904\u7406XML\u6587\u4ef6\u65f6\u7684\u5e38\u89c1\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u5904\u7406XML\u6587\u4ef6\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u6587\u4ef6\u683c\u5f0f\u4e0d\u6b63\u786e\u3001\u7f16\u7801\u95ee\u9898\u548c\u89e3\u6790\u9519\u8bef\u3002\u5982\u679c\u9047\u5230\u89e3\u6790\u9519\u8bef\uff0c\u53ef\u4ee5\u68c0\u67e5XML\u6587\u4ef6\u662f\u5426\u7b26\u5408\u6807\u51c6\u683c\u5f0f\uff0c\u786e\u4fdd\u6807\u7b7e\u6b63\u786e\u95ed\u5408\u3002\u7f16\u7801\u95ee\u9898\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6b63\u786e\u7684\u6587\u4ef6\u7f16\u7801\u6765\u89e3\u51b3\uff0c\u4f8b\u5982\u4f7f\u7528<code>utf-8<\/code>\u3002\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\uff0c\u60a8\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u5e76\u91c7\u53d6\u9002\u5f53\u7684\u63aa\u65bd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b89\u88c5XML\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528\u5185\u7f6e\u5e93\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93lxml\u3001\u4f7f\u7528BeautifulSoup\u3002 \u5728 [&hellip;]","protected":false},"author":3,"featured_media":1156385,"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\/1156377"}],"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=1156377"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156377\/revisions"}],"predecessor-version":[{"id":1156388,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156377\/revisions\/1156388"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1156385"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1156377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1156377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1156377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}