解析XML文件Python

时间:2018-06-29 17:33:28

标签: python xml parsing

作为一个相当新的Python学习者,我为自己设置了一个微型项目来解析Highways England xml文件。到目前为止,我的代码是:

#!/usr/bin/python 


import urllib2
varresponse = urllib2.urlopen('http://m.highwaysengland.co.uk/feeds/rss/AllEvents.xml')

from xml.etree import ElementTree as et
tree = et.parse(varresponse)

root = tree.getroot()

for item in root:
print(item.tag, item.attrib)


for author in root.iter('author')
    print author

当我在终端中运行该命令时,它仅显示作者一词,但如果需要的话,我希望它在作者括号内显示所有内容。 对我来说,下一步就是挑选关于M25的所有东西并打印出来,但是我也不完全知道该怎么做。

如果有人可以给我任何有关更改的建议,我将非常感激, 欢呼的家伙

2 个答案:

答案 0 :(得分:0)

如果您只需要作者标签内的电子邮件地址,请使用.text

例如:

for author in root.iter('author'):
    print author.text

输出:

info@highwaysengland.co.uk
info@highwaysengland.co.uk
info@highwaysengland.co.uk
info@highwaysengland.co.uk

答案 1 :(得分:0)

如果您想打印出<author>标签及其所有内容,请尝试以下操作:

print et.tostring(author)

使用您的示例输入,将产生:

>>> print et.tostring(author)
<author>info@highwaysengland.co.uk</author>

如果只需要文本,则可以使用.text属性,如下所示:

print author.text