I\'m trying to get all the XML attributes for the tag Name
.
Getting this error:
AttributeError: \'NoneType\' object has no attribute \'attr
In BeautifulSoup 4, you can use
doc = bs.BeautifulSoup(xml, "xml")
div = doc.find("Name")
This should work.
BeautifulSoup is a HTML-parsing library, primarily. It can handle XML too, but all tags are lowercased as per the HTML specification. Quoting the BeautifulSoup documentation:
Because HTML tags and attributes are case-insensitive, all three HTML parsers convert tag and attribute names to lowercase. That is, the markup
<TAG></TAG>
is converted to<tag></tag>
. If you want to preserve mixed-case or uppercase tags and attributes, you’ll need to parse the document as XML.
There is a XML modus where tags are matches case-sensitively and are not lowercased, but this requires the lxml
library to be installed. Because lxml
is a C-extension library, this is not supported on the Google App Engine.
Use the ElementTree API instead:
import xml.etree.ElementTree as ET
root = ET.fromstring(xml)
div = root.find('.//Name')
for attr, val in div.items():
print "%s:%s" % (attr, val)