我正在学习cElementTree,我的XML看起来像这样.我正在尝试获取“更新”文本(我可以!)以及"link“节点中"href”的属性值(我不能)。
<feed>
<entry>
<link href="http://www.mondocars.com/0001127602.htm"/>
<updated>2017-04-19T13:10:24-04:00</updated>
</entry>
</feed>我解析它的代码看起来是这样的..。
for entry in root.findall('entry'):
updated = entry.find('updated').text
print updated
for link in root.findall('link'):
href = link.get('href').attrib
print updated, href根本没有提取href值。我确信这可能是不必要的第二个循环。更新后的填充效果很好,但我不知道如何获得href值。有人遇到这个吗?
在此之前,非常感谢您。詹妮
发布于 2017-04-25 10:53:37
for entry in root.findall('entry'):
updated = entry.find('updated').text
href = entry.find('link').attrib.get('href')
print updated,href才是正确的方法。
https://stackoverflow.com/questions/43503971
复制相似问题