I'm trying to get all the XML attributes for the tag Name
.
Getting this error:
AttributeError: 'NoneType' object has no attribute 'attrs'
when I executed the following code:
import BeautifulSoup as bs
xml = '''
<Product Code="1" HighPic="http://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/Linksys48portswitch.jpg/220px-Linksys48portswitch.jpg" HighPicHeight="320" HighPicSize="37217" HighPicWidth="400" ID="35" Title="Demo Product">
<Category ID="23">
<Name ID="57" Value="Switches" langid="1"/>
</Category>
</Product>'''
doc = bs.BeautifulSoup(xml)
div = doc.find("Name")
for attr, val in div.attrs:
print "%s:%s" % (attr, val)
I changed the tag "Name"
to "name"
, and then it works.
Why am I getting this error when the tag name contains capital letters?
In BeautifulSoup 4, you can use
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:
There is a XML modus where tags are matches case-sensitively and are not lowercased, but this requires the
lxml
library to be installed. Becauselxml
is a C-extension library, this is not supported on the Google App Engine.Use the ElementTree API instead: