2

I am partway through pasing xml in python but have run into a bit of a stumbling block with the xml I am trying to render I am using xml.dom.minidom and I can get the xml no worries that bit is easy just the place I am getting it from is messing about with weird syntax what I am getting is something like this.

<enclosure name="link1" url="http://google.co.uk/vid.mp4" type="video/mp4" />

What I am strugling to do with the code bellow is get a way to extract the string from the url attribute I have googled and found nothing. I thought someone with more experience in xml would have an idea about how to do this. Bellow is the code I have so far for this section

files = urllib2.urlopen(x)
data = files.read()
files.close()

dom = parseString(data)

xmlTag = dom.getElementsByTagName('enclosure')[0].toxml()
print xmlTag
5
  • 1
    That looks more like HTML you're trying to parse - are you sure you're parsing XML? Commented Jul 20, 2014 at 22:10
  • yea I am sure it's XML it's a rss feed containing videos. I know the difference between HTML and XML. I used the name link as an example it's actually called enclosure. All the other elements are the same as XML Commented Jul 20, 2014 at 22:11
  • Heres what is at the top of the doc if you don't believe me <?xml version="1.0" encoding="UTF-8"?> Commented Jul 20, 2014 at 22:17
  • 1
    No worries - not like I didn't believe you - just that your original post had enough ambiguity to require clarification - and someone had to ask for that :) Commented Jul 20, 2014 at 22:21
  • Yeah suppose looking at it again I can see that now Commented Jul 20, 2014 at 22:27

1 Answer 1

2

You can use BeautifulSoup:

from BeautifulSoup import BeautifulSoup

xml = '<enclosure name="link1" url="http://google.co.uk/vid.mp4" type="video/mp4" />'
parsed_xml= BeautifulSoup(xml)
print parsed_xml.enclosure['url']

OUTPUT

http://google.co.uk/vid.mp4
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate didn't realise you could use beautiful soup that way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.