简体   繁体   中英

Parsing an XML file Python

As a pretty new Python learner, I have set myself a mini project to parse the highways England xml file. so far my code is:

#!/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

when I run this in terminal it only prints the word author but I want it to print everything within the author bracket if that makes any sense. also the next step for me is to pick out anything about the M25 and print that but I'm not completely sure how to do that either.

If anyone could give me any advice on what to change I would be so grateful, cheers guys

If you just need the email address inside the author tag use .text

Ex:

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

Output:

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

If you want to print out the <author> tag and all its contents, try something like:

print et.tostring(author)

With your sample input, that produces:

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

If you just want the text, you can use the .text attribute, as in:

print author.text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM