使用cElementTree来解析属性(Using cElementTree to parse attributes)

编程入门 行业动态 更新时间:2024-10-23 20:31:06
使用cElementTree来解析属性(Using cElementTree to parse attributes)

我正在学习cElementTree,我的XML看起来像这样....我试图获得“更新”的文本(我可以!)和“链接”节点中的“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值。 任何人遇到这个?

提前谢谢了。 珍妮

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 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?

Many thanks in advance. Janie

最满意答案

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

是正确的方法。

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

is the correct way.

更多推荐

本文发布于:2023-07-31 00:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340548.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   cElementTree   attributes   parse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!