17

我正在编写一个使用 编辑 XML 文件的脚本BeautifulStoneSoup,但该库将所有标签转换为小写。是否有保存案例的选项?

import BeautifulSoup    
xml = "<TestTag>a string</TestTag>"    
soup = BeautifulSoup.BeautifulStoneSoup(xml, markupMassage=False)    
print soup.prettify() # or soup.renderContents()
#prints
>>> <testtag>a string</testtag> 
#instead of the expected
>>> <TestTag>a string</TestTag>
4

1 回答 1

18

您可以使用Beautiful Soup 4,如下所示(需要 lxml XML 库):

In [10]: from bs4 import BeautifulSoup

In [11]: xml = "<TestTag>a string</TestTag>"

In [12]: soup = BeautifulSoup(xml, "xml")

In [13]: print soup
<?xml version="1.0" encoding="utf-8"?>
<TestTag>a string</TestTag>

In [14]:
于 2012-08-28T16:40:56.467 回答