I’m trying to use BeautifulSoup to parse some XML that looks like
<a>
<b>
<c>
<d attr="x">
<e>
</e>
<name>
</name>
</d>
</c>
<c>
...
<c>
</b>
</a>
but I can’t figure out how to access e or name in a loop. This works:
print soup.a.b.c.d.e
but this doesn’t:
for subtag in soup.a.b.c:
print subtag.d.e
instead it gives this error:
AttributeError: 'NavigableString' object has no attribute 'd'
and somewhat unrelatedly, this:
print soup.a.b.c.d.name
only outputs d.
What am I doing wrong? I suspect for the second issue I will have to use find() instead because that object already has a name attribute. Is there a nicer way?
You get an
AttributeErrorbecause BSS returnsNavigableStrings when you loop overTaginstances. Maybe you want to try:Regarding the issue w/
name: it is specified as an attribute, but you can instead do:Set-up code for reference: