Почему я не могу разобрать xml с помощью BeautifulSoup? - PullRequest
0 голосов
/ 19 апреля 2020

Я использую 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 все должно работать нормально. Но почему так?

1 Ответ

0 голосов
/ 19 апреля 2020

Не уверен насчет содержания вашего xml файла. Но с содержимым, которое вы публикуете, ваш код работает нормально:

from bs4 import BeautifulSoup
file_contents = '''<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>'''
soup = BeautifulSoup(file_contents,'lxml')
elements = soup.find_all('image')        #gives out an empty list
print(elements)

Вывод:

[<image>
<imagename>ryoungt_05.08.2002/aPICT0007.JPG</imagename>
<resolution x="1280" y="960"></resolution>
<taggedrectangles>
<taggedrectangle height="122.0" offset="0.0" rotation="0.0" username="admin" width="228.0" x="322.0" y="806.0"></taggedrectangle>
<taggedrectangle height="55.0" offset="0.0" rotation="0.0" username="admin" width="259.0" x="427.0" y="452.0"></taggedrectangle>
<taggedrectangle height="77.0" offset="0.0" rotation="0.0" username="admin" width="67.0" x="722.0" y="721.0"></taggedrectangle>
<taggedrectangle height="88.0" offset="0.0" rotation="0.0" username="admin" width="383.0" x="355.0" y="549.0"></taggedrectangle>
<taggedrectangle height="118.0" offset="0.0" rotation="0.0" username="admin" width="380.0" x="317.0" y="706.0"></taggedrectangle>
</taggedrectangles>
</image>, <image>
<imagename>ryoungt_05.08.2002/aPICT0010.JPG</imagename>
<resolution x="1280" y="960"></resolution>
<taggedrectangles>
<taggedrectangle height="84.0" offset="0.0" rotation="0.0" username="admin" width="351.0" x="594.0" y="663.0"></taggedrectangle>
<taggedrectangle height="72.0" offset="0.0" rotation="0.0" username="admin" width="206.0" x="346.0" y="792.0"></taggedrectangle>
<taggedrectangle height="87.0" offset="0.0" rotation="0.0" username="admin" width="243.0" x="310.0" y="659.0"></taggedrectangle>
<taggedrectangle height="88.0" offset="0.0" rotation="0.0" username="admin" width="308.0" x="599.0" y="797.0"></taggedrectangle>
</taggedrectangles>
</image>]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...