久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Python元素樹 - 從元素中提取文本,剝離標簽

Python element tree - extract text from element, stripping tags(Python元素樹 - 從元素中提取文本,剝離標簽)
本文介紹了Python元素樹 - 從元素中提取文本,剝離標簽的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

使用 Python 中的 ElementTree,如何從節點中提取所有文本,剝離該元素中的所有標簽并僅保留文本?

With ElementTree in Python, how can I extract all the text from a node, stripping any tags in that element and keeping only the text?

例如,假設我有以下內容:

For example, say I have the following:

<tag>
  Some <a>example</a> text
</tag>

我想返回一些示例文本.我該怎么做呢?到目前為止,我所采取的方法都產生了相當災難性的后果.

I want to return Some example text. How do I go about doing this? So far, the approaches I've taken have had fairly disastrous outcomes.

推薦答案

如果你在 Python 3.2+ 下運行,你可以使用 itertext.

If you are running under Python 3.2+, you can use itertext.

itertext 創建一個文本迭代器,它按文檔順序循環此元素和所有子元素,并返回所有內部文本:

itertext creates a text iterator which loops over this element and all subelements, in document order, and returns all inner text:

import xml.etree.ElementTree as ET
xml = '<tag>Some <a>example</a> text</tag>'
tree = ET.fromstring(xml)
print(''.join(tree.itertext()))

# -> 'Some example text'

如果你在較低版本的 Python 中運行,你可以重用 itertext() 的實現,通過將其附加到 Element 類,之后您可以像上面一樣調用它:

If you are running in a lower version of Python, you can reuse the implementation of itertext() by attaching it to the Element class, after which you can call it exactly like above:

# original implementation of .itertext() for Python 2.7
def itertext(self):
    tag = self.tag
    if not isinstance(tag, basestring) and tag is not None:
        return
    if self.text:
        yield self.text
    for e in self:
        for s in e.itertext():
            yield s
        if e.tail:
            yield e.tail

# if necessary, monkey-patch the Element class
if 'itertext' not in ET.Element.__dict__:
    ET.Element.itertext = itertext

xml = '<tag>Some <a>example</a> text</tag>'
tree = ET.fromstring(xml)
print(''.join(tree.itertext()))

# -> 'Some example text'

這篇關于Python元素樹 - 從元素中提取文本,剝離標簽的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
主站蜘蛛池模板: 亚洲精品欧美精品 | 亚洲精品国产电影 | 在线观看中文字幕视频 | 一区二区三区高清 | 亚洲欧美日韩国产综合 | 一本一道久久a久久精品综合 | 曰韩一二三区 | 亚洲国产视频一区二区 | 精品久久久久久久久亚洲 | 91精品国产91久久久久久最新 | 成人综合一区二区 | 亚洲一二三在线观看 | 国产在线观看一区二区 | 免费一级黄色电影 | 男女啪啪网址 | www.xxxx欧美| 欧美日韩第一页 | 一区二区不卡视频 | 欧美日韩久 | 99视频在线免费观看 | 91麻豆精品一区二区三区 | 国产成人精品免高潮在线观看 | 国产精品自产av一区二区三区 | 精品一区二区三区在线观看 | 日韩精品在线视频 | www..com18午夜观看 | 九九久久国产 | 中文字幕一区二区三区日韩精品 | 日韩中文字幕高清 | 久久久久国产精品免费免费搜索 | 免费特黄视频 | 亚洲电影一区二区三区 | 欧美全黄 | www.日日干| 亚洲成人激情在线观看 | 亚洲视频免费在线 | 人人九九精 | 国产污视频在线 | 欧美在线观看免费观看视频 | 中文字幕av亚洲精品一部二部 | 国产精品亚洲一区 |