{"id":952765,"date":"2024-12-27T01:35:54","date_gmt":"2024-12-26T17:35:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/952765.html"},"modified":"2024-12-27T01:35:56","modified_gmt":"2024-12-26T17:35:56","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%aelxml","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/952765.html","title":{"rendered":"python\u5982\u4f55\u8bbe\u7f6elxml"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090511\/4ca0538d-449c-48c7-94fd-521a1eb81b5e.webp\" alt=\"python\u5982\u4f55\u8bbe\u7f6elxml\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u8bbe\u7f6elxml\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u5b89\u88c5lxml\u5e93\u3001\u5bfc\u5165\u5e93\u3001\u89e3\u6790XML\u6216HTML\u6587\u4ef6\u3001\u4f7f\u7528XPath\u9009\u62e9\u5668\u3001\u5904\u7406\u89e3\u6790\u6811\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u70b9\uff1a\u5b89\u88c5lxml\u5e93\u3002lxml\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u5de5\u5177\u8f7b\u677e\u5b89\u88c5\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip install lxml<\/code>\u5373\u53ef\u5b8c\u6210\u5b89\u88c5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5lxml\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528lxml\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u8be5\u5e93\u5df2\u5b89\u88c5\u3002\u5b89\u88c5lxml\u5e93\u7684\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u3002\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u547d\u4ee4\u4f1a\u81ea\u52a8\u4e0b\u8f7d\u5e76\u5b89\u88c5lxml\u53ca\u5176\u6240\u6709\u4f9d\u8d56\u9879\u3002\u5982\u679c\u60a8\u5728\u5b89\u88c5\u65f6\u9047\u5230\u56f0\u96be\uff0c\u53ef\u80fd\u9700\u8981\u786e\u4fdd\u60a8\u7684Python\u548cpip\u7248\u672c\u90fd\u662f\u6700\u65b0\u7684\uff0c\u5e76\u4e14\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684\u6784\u5efa\u5de5\u5177\u3002lxml\u4f9d\u8d56\u4e8eC\u8bed\u8a00\u7684libxml2\u548clibxslt\u5e93\uff0c\u56e0\u6b64\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u8fd9\u4e9b\u5e93\u7684\u5f00\u53d1\u7248\u672c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5bfc\u5165lxml\u5e93<\/p>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5c31\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165lxml\u5e93\u3002\u901a\u5e38\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5bfc\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from lxml import etree<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>etree\u6a21\u5757\u63d0\u4f9b\u4e86\u7528\u4e8e\u89e3\u6790\u548c\u521b\u5efaXML\u548cHTML\u6587\u6863\u7684\u7c7b\u548c\u51fd\u6570\u3002\u5bfc\u5165\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528etree\u6a21\u5757\u4e2d\u7684\u529f\u80fd\u6765\u5904\u7406XML\u548cHTML\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u89e3\u6790XML\u6216HTML\u6587\u4ef6<\/p>\n<\/p>\n<p><p>lxml\u652f\u6301\u591a\u79cd\u65b9\u5f0f\u89e3\u6790XML\u548cHTML\u6587\u4ef6\uff0c\u6700\u5e38\u89c1\u7684\u662f\u4ece\u5b57\u7b26\u4e32\u3001\u6587\u4ef6\u6216URL\u8fdb\u884c\u89e3\u6790\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4ece\u5b57\u7b26\u4e32\u89e3\u6790\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">xml_string = &quot;&lt;root&gt;&lt;child&gt;data&lt;\/child&gt;&lt;\/root&gt;&quot;<\/p>\n<p>root = etree.fromstring(xml_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4ece\u6587\u4ef6\u89e3\u6790\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">tree = etree.parse(&#39;example.xml&#39;)<\/p>\n<p>root = tree.getroot()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4eceURL\u89e3\u6790\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;http:\/\/example.com\/example.xml&#39;)<\/p>\n<p>root = etree.fromstring(response.content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u4ece\u5b57\u7b26\u4e32\u3001\u6587\u4ef6\u8fd8\u662fURL\u89e3\u6790\uff0c\u6700\u7ec8\u90fd\u4f1a\u5f97\u5230\u4e00\u4e2aElement\u5bf9\u8c61\u6216ElementTree\u5bf9\u8c61\uff0c\u5b83\u4eec\u662flxml\u7684\u6838\u5fc3\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528XPath\u9009\u62e9\u5668<\/p>\n<\/p>\n<p><p>XPath\u662f\u4e00\u79cd\u7528\u4e8e\u5728XML\u6587\u6863\u4e2d\u8fdb\u884c\u5bfc\u822a\u7684\u8bed\u8a00\u3002lxml\u63d0\u4f9b\u4e86\u5bf9XPath\u7684\u5f3a\u5927\u652f\u6301\uff0c\u4f7f\u5f97\u4eceXML\u6216HTML\u6587\u6863\u4e2d\u63d0\u53d6\u6570\u636e\u53d8\u5f97\u975e\u5e38\u5bb9\u6613\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bberoot\u662f\u5df2\u7ecf\u89e3\u6790\u7684XML\u6216HTML\u6811\u7684\u6839\u8282\u70b9<\/p>\n<p>result = root.xpath(&#39;\/\/child&#39;)<\/p>\n<p>for element in result:<\/p>\n<p>    print(element.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>\/\/child<\/code>\u662f\u4e00\u4e2aXPath\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u9009\u62e9\u6240\u6709\u540d\u4e3a<code>child<\/code>\u7684\u5143\u7d20\u3002XPath\u652f\u6301\u590d\u6742\u7684\u67e5\u8be2\uff0c\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u3001\u51fd\u6570\u7b49\u8fdb\u884c\u7075\u6d3b\u7684\u6570\u636e\u63d0\u53d6\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u89e3\u6790\u6811<\/p>\n<\/p>\n<p><p>\u89e3\u6790\u6811\u662flxml\u7684\u6838\u5fc3\u6982\u5ff5\u4e4b\u4e00\uff0c\u901a\u8fc7Element\u5bf9\u8c61\u548cElementTree\u5bf9\u8c61\u6765\u8868\u793a\u3002Element\u5bf9\u8c61\u8868\u793aXML\u6216HTML\u6587\u6863\u4e2d\u7684\u5355\u4e2a\u5143\u7d20\uff0c\u800cElementTree\u5bf9\u8c61\u8868\u793a\u6574\u4e2a\u6587\u6863\u3002<\/p>\n<\/p>\n<ol>\n<li>\u904d\u5386\u89e3\u6790\u6811\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">for element in root.iter():<\/p>\n<p>    print(element.tag, element.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4fee\u6539\u89e3\u6790\u6811\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">new_element = etree.Element(&#39;new_child&#39;)<\/p>\n<p>new_element.text = &#39;new data&#39;<\/p>\n<p>root.append(new_element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u8f93\u51fa\u89e3\u6790\u6811\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">print(etree.tostring(root, pretty_print=True).decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u60a8\u53ef\u4ee5\u5bf9\u89e3\u6790\u6811\u8fdb\u884c\u904d\u5386\u3001\u4fee\u6539\u548c\u8f93\u51fa\uff0c\u6ee1\u8db3\u5404\u79cd\u6570\u636e\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u9519\u8bef\u5904\u7406\u4e0e\u6027\u80fd\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528lxml\u65f6\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u9519\u8bef\u5904\u7406\u548c\u6027\u80fd\u4f18\u5316\u3002\u89e3\u6790\u548c\u5904\u7406\u5927\u578bXML\u6216HTML\u6587\u6863\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5185\u5b58\u4e0d\u8db3\u6216\u6027\u80fd\u74f6\u9888\u7684\u95ee\u9898\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u8fdb\u884c\u4f18\u5316\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528iterparse\u8fdb\u884c\u589e\u91cf\u89e3\u6790\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">for event, element in etree.iterparse(&#39;large.xml&#39;, events=(&#39;end&#39;,)):<\/p>\n<p>    print(element.tag, element.text)<\/p>\n<p>    element.clear()  # \u91ca\u653e\u5185\u5b58<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528DTD\u9a8c\u8bc1\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">dtd = etree.DTD(&#39;example.dtd&#39;)<\/p>\n<p>if dtd.validate(root):<\/p>\n<p>    print(&quot;Document is valid&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Document is invalid&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u6355\u83b7\u89e3\u6790\u9519\u8bef\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    root = etree.fromstring(xml_string)<\/p>\n<p>except etree.XMLSyntaxError as e:<\/p>\n<p>    print(f&quot;XML syntax error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8lxml\u7684\u4f7f\u7528\u6548\u7387\uff0c\u5e76\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u63d0\u4f9b\u6709\u7528\u7684\u53cd\u9988\u4fe1\u606f\u3002\u603b\u7684\u6765\u8bf4\uff0clxml\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u5e93\uff0c\u9002\u7528\u4e8e\u5404\u79cdXML\u548cHTML\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5b89\u88c5lxml\u5e93\u4ee5\u4fbf\u5728Python\u4e2d\u4f7f\u7528\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4f7f\u7528lxml\u5e93\uff0c\u60a8\u9700\u8981\u901a\u8fc7pip\u5b89\u88c5\u5b83\u3002\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<code>pip install lxml<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165lxml\u6a21\u5757\u4ee5\u5f00\u59cb\u4f7f\u7528\u3002<\/p>\n<p><strong>\u4f7f\u7528lxml\u89e3\u6790HTML\u548cXML\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u4f7f\u7528lxml\u89e3\u6790HTML\u6216XML\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u5bfc\u5165lxml\u5e93\u4e2d\u7684etree\u6a21\u5757\u3002\u63a5\u7740\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>etree.HTML()<\/code>\u6216<code>etree.XML()<\/code>\u65b9\u6cd5\u6765\u89e3\u6790HTML\u6216XML\u6587\u6863\u3002\u89e3\u6790\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528XPath\u6216CSS\u9009\u62e9\u5668\u6765\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002<\/p>\n<p><strong>lxml\u4e0e\u5176\u4ed6Python\u5e93\u76f8\u6bd4\u6709\u54ea\u4e9b\u4f18\u52bf\uff1f<\/strong><br \/>lxml\u5e93\u4ee5\u5176\u9ad8\u6548\u7684\u6027\u80fd\u548c\u4e30\u5bcc\u7684\u529f\u80fd\u800c\u8457\u79f0\u3002\u4e0e\u5176\u4ed6\u5e93\u76f8\u6bd4\uff0clxml\u652f\u6301XPath\u548cXSLT\uff0c\u8fd9\u4f7f\u5f97\u5904\u7406\u590d\u6742\u7684XML\u6570\u636e\u66f4\u52a0\u7075\u6d3b\u3002\u540c\u65f6\uff0clxml\u5bf9\u4e8e\u89e3\u6790\u5927\u578b\u6587\u6863\u7684\u901f\u5ea6\u4e5f\u76f8\u5bf9\u8f83\u5feb\uff0c\u9002\u5408\u5904\u7406\u9700\u8981\u9ad8\u6548\u89e3\u6790\u7684\u9879\u76ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8bbe\u7f6elxml\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u5b89\u88c5lxml\u5e93\u3001\u5bfc\u5165\u5e93\u3001\u89e3\u6790XML\u6216HTML\u6587\u4ef6\u3001\u4f7f\u7528XPath\u9009\u62e9 [&hellip;]","protected":false},"author":3,"featured_media":952768,"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\/952765"}],"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=952765"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/952765\/revisions"}],"predecessor-version":[{"id":952772,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/952765\/revisions\/952772"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/952768"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=952765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=952765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=952765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}