Tag.name
is an attribute containing the tag name; it's value here is row_stud
.
Attribute access to contained tags is a shortcut for .find(attributename)
, but only works if there isn't already an attribute in the API with the same name. Use .find()
instead:
print stud_info[eachStud].find('name').get_text()
You can loop over the stud_info
result list directly, no need to use range()
here:
stud_info = output_xml.find_all('row_stud')
for eachStud in stud_info:
print eachStud.dept.get_text()
print eachStud.find('name').get_text()
I notice that you are searching for row_stud
in lower-case. If you are parsing XML with BeautifulSoup, make sure that you have lxml
installed and tell BeautifulSoup it is XML you are processing, so that it won't HTML-ize your tags (lowercase them):
soup = BeautifulSoup(source, 'xml')