使用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, hrefhref值完全没有被拉动。 我相信这可能是一个不必要的第二个循环。 更新填充罚款,但我不知道如何获得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, hrefhref 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,hrefis the correct way.
更多推荐
发布评论