Python 使用BeautifulSoup解析XML时出现Unicode对象错误
使用BeautifulSoup分析XML输出中“name”标记的内容时,会出现以下错误:Python 使用BeautifulSoup解析XML时出现Unicode对象错误,python,xml,unicode,beautifulsoup,Python,Xml,Unicode,Beautifulsoup,使用BeautifulSoup分析XML输出中“name”标记的内容时,会出现以下错误: AttributeError: 'unicode' object has no attribute 'get_text' XML输出: <show> <stud> <__readonly__> <TABLE_stud> <ROW_stud> <name>rice</na
AttributeError: 'unicode' object has no attribute 'get_text'
XML输出:
<show>
<stud>
<__readonly__>
<TABLE_stud>
<ROW_stud>
<name>rice</name>
<dept>chem</dept>
.
.
.
</ROW_stud>
</TABLE_stud>
</__readonly__>
</stud>
</show>
任何python/BeautifulSoup专家都能帮我解决这个问题吗?
(我知道BeautifulSoup不适合解析XML。但我不得不说我不得不使用它)是一个包含标记名的属性;这里的值是行\u stud
对包含的标记的属性访问是.find(attributename)
的快捷方式,但仅当API中没有同名属性时才有效。改用.find()
:
print stud_info[eachStud].find('name').get_text()
您可以直接在stud\u info
结果列表上循环,无需在此处使用range()
:
stud_info = output_xml.find_all('row_stud')
for eachStud in stud_info:
print eachStud.dept.get_text()
print eachStud.find('name').get_text()
我注意到您正在搜索小写的行。如果您正在使用BeautifulSoup解析XML,请确保已安装lxml
,并告诉BeautifulSoup您正在处理的是XML,这样它就不会将标记HTML化(小写):
对于范围内的每个stud(len(stud\u info))
是一种反模式,直接迭代stud\u info的元素。
stud_info = output_xml.find_all('row_stud')
for eachStud in stud_info:
print eachStud.dept.get_text()
print eachStud.find('name').get_text()
soup = BeautifulSoup(source, 'xml')