일부 데이터를 추출하려면 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로 가장 효율적이고 우아한 방법은 무엇입니까?
나는 찾기 방법으로 시도했지만 그것은 아주 좋은 아니에요 :
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