lxml怎么删除namespaces

发布于 2022-09-06 09:52:13 字数 2019 浏览 13 评论 0

我读取一个xhtml

<?xml version='1.0' encoding='utf-8'?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="calibre:cover" content="true"/>
        <title>Cover</title>
        <style type="text/css" title="override_css">
            @page {padding: 0pt; margin:0pt}
            body { text-align: center; padding:0pt; margin: 0pt; }
        </style>
    </head>
    <body>
        <div>
            <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="100%" height="100%" viewBox="0 0 200 266" preserveAspectRatio="none">
                <image width="200" height="266" xlink:href="cover1.jpeg"/>
            </svg>
        </div>
    </body>
</html>

把一个xhtml里的body里的保存到新的html

from lxml import etree
with open("test.xhtml", 'r', encoding='utf8') as html:
    tree = etree.parse(html)
    body = tree.find(
        '//xmlns:body',
         namespaces={'xmlns': 'http://www.w3.org/1999/xhtml'}
    )
    nsmap = body.nsmap
    # 这里不加nsmap所有标签都会有namespaces
    page_xml = etree.Element('div', nsmap=nsmap)
    for child in body.iterchildren():
        page_xml.append(child)
    etree.ElementTree(page_xml).write(
        "new.html",
        pretty_print=True,
        encoding='utf-8',
        method='html'
    )

最后转换出来new.html多了一个xmlns,问题来了怎么去掉呢?

<div xmlns="http://www.w3.org/1999/xhtml">
    <div>
        <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="100%" height="100%" viewBox="0 0 200 266" preserveAspectRatio="none">
            <image width="200" height="266" xlink:href="cover1.jpeg"/>
        </svg>
    </div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南月 2022-09-13 09:52:14

HTML 处理就不会带名字空间:

# -*- coding: utf-8 -*-

from lxml import etree

content = '''
<?xml version='1.0' encoding='utf-8'?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="calibre:cover" content="true"/>
        <title>Cover</title>
        <style type="text/css" title="override_css">
            @page {padding: 0pt; margin:0pt}
            body { text-align: center; padding:0pt; margin: 0pt; }
        </style>
    </head>
    <body>
        <div>
            <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="100%" height="100%" viewBox="0 0 200 266" preserveAspectRatio="none">
                <image width="200" height="266" xlink:href="cover1.jpeg"/>
            </svg>
        </div>
    </body>
</html>
'''

print etree.tostring(etree.HTML(content).xpath('//body/*')[0])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文