いくつかのデータを抽出するためにxmlファイルを解析する必要があります。 私は特定の属性を持ついくつかの要素を必要とし、ここでの文書の例です:lxmlの属性で要素を見つける
<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>
ここで私はタイプ「ニュース」とだけ記事を取得したいと思います。 lxmlで行うのが最も効率的でエレガントな方法は何ですか?
私は、findメソッドで試してみましたが、それは非常に素晴らしいではありません。
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