簡體   English   中英

如何使用文本內的標簽解析xml語句

[英]How to parse xml statement with tags inside the text

如何在python中解析以下內容以獲得<p>括號之間的文本,但沒有<mark>標記或值? 最好使用ElementTree功能

<plist>
<p>Hello there? <mark type="ph"/> How are you?</p>
</plist>

解析以下內容應返回“ Hello There? 你好嗎?

謝謝!

對於lxml.etree ,可以使用string() XPath函數:

from lxml.etree import fromstring

data = """
<plist>
<p>Hello there? <mark type="ph"/> How are you?</p>
</plist>
"""

root = fromstring(data)
for p in root.xpath("//plist/p"):
    print p.xpath("string()")

打印:

Hello there?  How are you?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM