elementtree - 获得标题?

elementtree - get title?

<item rdf:about="http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-0005">    
<title>CVE-2014-0005</title>
<link>http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-0005</link>
<description>PicketBox and JBossSX, as used in Red Hat JBoss Enterprise Application Platform (JBEAP) 6.2.2 and JBoss BRMS before 6.0.3 roll up patch 2, allows remote authenticated users to read and modify the application sever configuration and state by deploying a crafted application.</description>
<dc:date>2015-02-20T16:59:00Z</dc:date>
</item>
<item rdf:about="http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-1831">
<title>CVE-2014-1831 (passenger)</title>
<link>http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-1831</link>
<description>Phusion Passenger before 4.0.37 allows local users to write to certain files and directories via a symlink attack on (1) control_process.pid or a (2) generation-* file.</description>
<dc:date>2015-02-19T15:59:02Z</dc:date>
</item>

嗨,

鉴于上述情况,我正在尝试从 xml 文件的标题项中提取文本值。但是,以下内容不起作用(没有结果)。请指教

def processxml():
    tree = ET.parse('nvd-rss.xml')
    for item in tree.findall( 'item/title' ):
        print (title.text)

提前致谢

正如 BorrajaX 提到的,您需要将 for 循环中的代码更改为 print(item.text),因为您遍历了所有标签 ('elements') findall() -ElementTree 模块的方法returns。您可以通过读取 ElementTree 元素实例的属性 text 来获取元素内的文本。

# create an ElementTree instance called tree

for element in tree.findall( 'item/title' ):
    print(element.text)

ElementTree 元素实例的一些其他属性:

  • .tag 元素名称
  • .text 元素内的文本
  • .tail 元素后的文本
  • .attrib 包含所有元素的属性名称及其对应值的字典