如何让Python bs4在XML上正常工作?

时间:2016-03-22 00:32:52

标签: python xml unicode beautifulsoup bs4

我尝试使用Python和BeautifulSoup 4(bs4)将Inkscape SVG转换为类似XML的格式,用于某些专有软件。我似乎无法让bs4正确解析一个最小的例子。我需要解析器尊重自闭标签,处理unicode,而不是添加html东西。我想要指定' lxml'使用selfClosingTags的解析器应该这样做,但是没有!看看吧。

#!/usr/bin/python
from __future__ import print_function
from bs4 import BeautifulSoup

print('\nbs4 mangled XML:')
print(BeautifulSoup('<x><c name="b1"><d value="a"/></c></x>',
    features = "lxml", 
    selfClosingTags = ('d')).prettify())

print('''\nExpected output:
<x>
 <c name="b1">
  <d value="a"/>
 </c>
</x>''')

打印

bs4 mangled XML:
/usr/local/lib/python2.7/dist-packages/beautifulsoup4-4.4.1-py2.7.egg/bs4/__init__.py:112: UserWarning: BS4 does not respect the selfClosingTags argument to the BeautifulSoup constructor. The tree builder is responsible for understanding self-closing tags.
<html>
 <body>
  <x>
   <c name="b1">
    <d value="a">
    </d>
   </c>
  </x>
 </body>
</html>

Expected output:
<x>
 <c name="b1">
  <d value="a"/>
 </c>
</x>

我已经回顾了相关的StackOverflow问题,但我找不到解决方案。

This question解决了html样板,但仅用于解析html的子部分,而不是用于解析xml。

This question涉及获得beautifulsoup 4以尊重自闭标签,并且没有接受的答案。

This question似乎表明传递selfClosingTags参数应该会有所帮助,但正如您所看到的,现在会生成警告BS4 does not respect the selfClosingTags argument,并且自动关闭标记会被破坏。

This question建议使用&#34; xml&#34; (不是&#34; lxml&#34;)会导致空标签自动关闭。这个可能可以用于我的目的,但应用&#34; xml&#34;解析器到我的实际数据失败,因为文件包含unicode,其中&#34; xml&#34;解析器不支持。

是&#34; xml&#34;与&#34; lxml&#34;不同,并且标准是&#34; xml&#34; 不能支持unicode,&#34; lxml&#34; 无法包含自闭标签?也许我只是想做一些被禁止的事情?

1 个答案:

答案 0 :(得分:1)

如果您希望将结果输出为xml,则将其解析为。您的xml数据可以包含unicode,但是,您需要声明编码:

#!/usr/bin/env python
# -*- encoding: utf8 -*-
  

The SelfClosingTags is no longer recognized。相反,美丽   Soup将任何空标记视为空元素标记。如果你添加一个   如果子元素为空元素标记,则它不再是空元素标记。

将您的功能更改为此类应该有效(除了编码):

print(BeautifulSoup('<x><c name="b1"><d value="a®"/></c></x>',
    features = "xml").prettify())

<强>结果:

<?xml version="1.0" encoding="utf-8"?>
<x>
 <c name="b1">
  <d value="aÂŽ"/>
 </c>
</x>