Reputation: 8411
My xml looks like this, I want to get the locations.
<?xml version="1.0" encoding="UTF-8"?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
<trackList>
<track>
<location>file:///home/ashu/Music/Collections/randomPicks/ipod%20on%20sep%2009/Coldplay-Sparks.mp3</location>
<title>Coldplay-Sparks</title>
</track>
<track>
<location>file:///home/ashu/Music/Collections/randomPicks/gud%201s/Coldplay%20Warning%20sign.mp3</location>
<title>Coldplay Warning sign</title>
</track>....
I am trying:
from BeautifulSoup import BeautifulSoup as bs
soup = bs (the_above_xml_text)
for track in soup.tracklist:
print track.location.string
But that is not working because I am getting:
AttributeError: 'NavigableString' object has no attribute 'location'
How can I achieve the result, thanks in advance.
Upvotes: 0
Views: 1269
Reputation: 55448
You can use findAll
:
>>> for track in soup.findAll('track'):
... print track.title.string
... print track.location.string
...
Coldplay-Sparks
file:///home/ashu/Music/Collections/randomPicks/ipod%20on%20sep%2009/Coldplay-Sparks.mp3
Coldplay Warning sign
file:///home/ashu/Music/Collections/randomPicks/gud%201s/Coldplay%20Warning%20sign.mp3
Upvotes: 0
Reputation: 223062
Use lxml, it's faster and supports xpath:
>>> doc = lxml.etree.fromstring(yourxml)
>>> doc.xpath('//n:location/text()', namespaces={'n': 'http://xspf.org/ns/0/'})
['file:///home/ashu/Music/Collections/randomPicks/ipod%20on%20sep%2009/Coldplay-Sparks.mp3',
'file:///home/ashu/Music/Collections/randomPicks/gud%201s/Coldplay%20Warning%20sign.mp3']
Upvotes: 1