通过LXML属性找到元素 [英] finding elements by attribute with lxml

查看:535
本文介绍了通过LXML属性找到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析XML文件中提取一些数据。
我只需要具有特定属性的一些元素,这里有一个文件为例:

 <根和GT;
    <文章>
        <物品类=新闻>
             <内容>有的文字< /内容>
        < /条>
        <物品类=信息>
             <内容>有的文字< /内容>
        < /条>
        <物品类=新闻>
             <内容>有的文字< /内容>
        < /条>
    < /条>
< /根>

在这里,我想获得只与类型新闻的文章。
什么是最有效和最优雅的方式与LXML做到这一点?

我试着用find方法,但它不是很漂亮:

 从LXML进口etree
F =调用etree.parse(MYFILE)
根= f.getroot()
物品= root.getchildren()[0]
article_list = articles.findall('文章')
在article_list文章:
    如果article.keys输入():
        如果article.attrib [型] ==新闻:
            内容= article.find(内容)
            内容= content.text


解决方案

您可以使用XPath,例如 root.xpath(//文章[@类型='新闻'])

此XPath前pression将返回所有℃的名单;文章/> 与型与价值新闻属性的元素。然后,您可以遍历它做你想要什么,或者通过它的地方。

要得到的只是文字内容,您可以扩展的XPath像这样:

 根= etree.fromstring(
<根和GT;
    <文章>
        <物品类=新闻>
             <内容>有的文字< /内容>
        < /条>
        <物品类=信息>
             <内容>有的文字< /内容>
        < /条>
        <物品类=新闻>
             <内容>有的文字< /内容>
        < /条>
    < /条>
< /根>
)打印root.xpath(//文章[@类型='新闻'] /内容/文本())

这将输出 ['一些文本','一些文本'] 。或者,如果你只是想的内容元素,这将是//文章[@类型='新闻'] /内容 - 等等

I need to parse a xml file to extract some data. I only need some elements with certain attributes, here's an exemple of document:

<root>
    <articles>
        <article type="news">
             <content>some text</content>
        </article>
        <article type="info">
             <content>some text</content>
        </article>
        <article type="news">
             <content>some text</content>
        </article>
    </articles>
</root>

Here i would like to get only the article with the type "news". What's the most efficient and elegant way to do it with lxml???

I tried with the find method but it's not very nice:

from lxml import etree
f = etree.parse("myfile")
root = f.getroot()
articles = root.getchildren()[0]
article_list = articles.findall('article')
for article in article_list:
    if "type" in article.keys():
        if article.attrib['type'] == 'news':
            content = article.find('content')
            content = content.text

解决方案

You can use xpath, e.g. root.xpath("//article[@type='news']")

This xpath expression will return a list of all <article/> elements with "type" attributes with value "news". You can then iterate over it to do what you want, or pass it wherever.

To get just the text content, you can extend the xpath like so:

root = etree.fromstring("""
<root>
    <articles>
        <article type="news">
             <content>some text</content>
        </article>
        <article type="info">
             <content>some text</content>
        </article>
        <article type="news">
             <content>some text</content>
        </article>
    </articles>
</root>
""")

print root.xpath("//article[@type='news']/content/text()")

and this will output ['some text', 'some text']. Or if you just wanted the content elements, it would be "//article[@type='news']/content" -- and so on.

这篇关于通过LXML属性找到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆