使用cElementTree解析属性 [英] Using cElementTree to parse attributes

查看:75
本文介绍了使用cElementTree解析属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习cElementTree,我的XML看起来像这样....我正在尝试获取更新的"文本(可以!)和链接"节点中的"href"的属性值(我可以不能).

I am learning cElementTree and my XML looks like this.... I am trying to obtain the "updated" text ( which I can! ) and the attribute value of "href" in the "link" node ( which I can't ).

<feed>
    <entry>
        <link href="http://www.mondocars.com/0001127602.htm"/>
        <updated>2017-04-19T13:10:24-04:00</updated>
    </entry>
</feed>

我解析它的代码看起来像这样...

My code to parse it looks like this...

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值根本没有被提取.我坚信这可能是不必要的2nd for循环.更新填充很好,但我不知道如何获取href值.有人遇到这个吗?

href value isn't being pulled at all. I am convinced that it's probably an unnecessary 2nd for loop. updated populates fine but I can't figure out how to get the href value. Anyone encounter this?

非常感谢. 珍妮

推荐答案

for entry in root.findall('entry'):         
    updated = entry.find('updated').text
    href = entry.find('link').attrib.get('href')
    print updated,href

是正确的方法.

这篇关于使用cElementTree解析属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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