第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

剝離一些標(biāo)簽并重命名它們

剝離一些標(biāo)簽并重命名它們

慕森卡 2022-06-02 15:32:20
使用 lxml 庫,擁有這個 doc xml 文件,我想剝離一些標(biāo)簽并重命名它們:doc.xml<html>    <body>        <h5>Fruits</h5>        <div>This is some <span attr="foo">Text</span>.</div>        <div>Some <span>more</span> text.</div>        <h5>Vegetables</h5>        <div>Yet another line <span attr="bar">of</span> text.</div>        <div>This span will get <span attr="foo">removed</span> as well.</div>        <div>Nested elements <span attr="foo">will <b>be</b> left</span> alone.</div>        <div>Unless <span attr="foo">they <span attr="foo">also</span> match</span>.</div>    </body></html>而不是 html,body 將所有內(nèi)容包裝在“p tag”中,而不是讓 h5 和每個 div 使用 lxml 將所有內(nèi)容作為示例包裝如下:我的問題是如何從一種格式以下面的格式包裝所有內(nèi)容?<p><h5 title='Fruits'> <div>This is some <span attr='foo'>Test</span>.</div><div>Some<span>more</span>text.</div></h5><h5 title='Vegetables'><div>Yet another line <span attr='bar'>of</span>text.</div>....</h5></p>使用 lxml,剝離標(biāo)簽:tree = etree.tostring(doc.xml)tree1 = lxml.html.fromstring(tree)etree.strip_tags(tree1, 'body')有人對此有任何想法嗎?
查看完整描述

2 回答

?
皈依舞

TA貢獻(xiàn)1851條經(jīng)驗 獲得超3個贊

  • 創(chuàng)建一個只有標(biāo)簽的新文檔。<p>

  • 迭代<body>原始文檔中標(biāo)記的后代。

    • 如果遇到<h5>標(biāo)簽;將<h5>標(biāo)簽添加到<p>標(biāo)簽

    • 并將后續(xù)標(biāo)簽作為后代添加到它(<h5>

    • 將標(biāo)簽從原始文檔添加到新文檔 - 作為其<p>標(biāo)簽 的后代


    查看完整回答
    反對 回復(fù) 2022-06-02
    ?
    至尊寶的傳說

    TA貢獻(xiàn)1789條經(jīng)驗 獲得超10個贊

    這是使用 lxml 的 xslt 解決方案。它將處理卸載到 libxml。我在轉(zhuǎn)換樣式表中添加了注釋:


    from lxml import etree


    xsl = etree.XML('''

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

        <xsl:output method="xml" indent="yes" />

        <xsl:strip-space elements="*"/>


        <xsl:template match="/">

            <p>

                <xsl:apply-templates select="html/body"/>

            </p>

        </xsl:template>


        <!-- match body, but do not add content; this excludes /html/body elements -->

        <xsl:template match="body">

            <xsl:apply-templates />

        </xsl:template>


        <xsl:template match="h5">

            <!-- record the current h5 title -->

            <xsl:variable name="title" select="."/>

            <h5>

                <xsl:attribute name="title">

                    <xsl:value-of select="$title" />

                </xsl:attribute>


                <xsl:for-each select="following-sibling::div[preceding-sibling::h5[1] = $title]">

                    <!-- deep copy of each consecutive div following the current h5 element -->

                    <xsl:copy-of select="." />

                </xsl:for-each>

            </h5>

        </xsl:template>


        <!-- match div, but do not output anything since we are copying it into the new h5 element -->

        <xsl:template match="div" />

    </xsl:stylesheet>

    ''')


    transform = etree.XSLT(xsl)

    with open("doc.xml") as f:

        print(transform(etree.parse(f)), end='')

    如果樣式表存儲在文件名 doc.xsl 中,則可以使用 libxml 實用程序 xsltproc 獲得相同的結(jié)果:


    xsltproc doc.xsl doc.xml

    結(jié)果:


    <?xml version="1.0"?>

    <p>

      <h5 title="Fruits">

        <div>This is some <span attr="foo">Text</span>.</div>

        <div>Some <span>more</span> text.</div>

      </h5>

      <h5 title="Vegetables">

        <div>Yet another line <span attr="bar">of</span> text.</div>

        <div>This span will get <span attr="foo">removed</span> as well.</div>

        <div>Nested elements <span attr="foo">will <b>be</b> left</span> alone.</div>

        <div>Unless <span attr="foo">they <span attr="foo">also</span> match</span>.</div>

      </h5>

    </p>


    查看完整回答
    反對 回復(fù) 2022-06-02
    • 2 回答
    • 0 關(guān)注
    • 126 瀏覽
    慕課專欄
    更多

    添加回答

    舉報

    0/150
    提交
    取消
    微信客服

    購課補(bǔ)貼
    聯(lián)系客服咨詢優(yōu)惠詳情

    幫助反饋 APP下載

    慕課網(wǎng)APP
    您的移動學(xué)習(xí)伙伴

    公眾號

    掃描二維碼
    關(guān)注慕課網(wǎng)微信公眾號