Я использую BeautifulSoup для анализа моего XML документа. Однако стандартные команды, которые работают для HTML, не работают для XML (например, метод soup.find_all()
). Почему так?
from bs4 import BeautifulSoup
file =open("locations.xml",'r')
file_contents = file.read()
soup = BeautifulSoup(file_contents,'lxml')
elements = soup.find_all('image') #gives out an empty list
print(soup.tag) #prints my xml document
<image>
<imageName>ryoungt_05.08.2002/aPICT0007.JPG</imageName>
<resolution x="1280" y="960" />
<taggedRectangles>
<taggedRectangle x="322.0" y="806.0" width="228.0" height="122.0" offset="0.0" rotation="0.0" userName="admin" />
<taggedRectangle x="427.0" y="452.0" width="259.0" height="55.0" offset="0.0" rotation="0.0" userName="admin" />
<taggedRectangle x="722.0" y="721.0" width="67.0" height="77.0" offset="0.0" rotation="0.0" userName="admin" />
<taggedRectangle x="355.0" y="549.0" width="383.0" height="88.0" offset="0.0" rotation="0.0" userName="admin" />
<taggedRectangle x="317.0" y="706.0" width="380.0" height="118.0" offset="0.0" rotation="0.0" userName="admin" />
</taggedRectangles>
</image>
<image>
<imageName>ryoungt_05.08.2002/aPICT0010.JPG</imageName>
<resolution x="1280" y="960" />
<taggedRectangles>
<taggedRectangle x="594.0" y="663.0" width="351.0" height="84.0" offset="0.0" rotation="0.0" userName="admin" />
<taggedRectangle x="346.0" y="792.0" width="206.0" height="72.0" offset="0.0" rotation="0.0" userName="admin" />
<taggedRectangle x="310.0" y="659.0" width="243.0" height="87.0" offset="0.0" rotation="0.0" userName="admin" />
<taggedRectangle x="599.0" y="797.0" width="308.0" height="88.0" offset="0.0" rotation="0.0" userName="admin" />
</taggedRectangles>
</image>
Согласно документации BeautifulSoup, после установки парсера l xml все должно работать нормально. Но почему так?