Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用BeautifulSoup解析XML时出现Unicode对象错误_Python_Xml_Unicode_Beautifulsoup - Fatal编程技术网

Python 使用BeautifulSoup解析XML时出现Unicode对象错误

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

使用BeautifulSoup分析XML输出中“name”标记的内容时,会出现以下错误:

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')