如何在BeautifulSoup.BeautifulStoneSoup中维护区分大小写的标签?

Tan*_*ash 12 python xml beautifulsoup

我正在编写一个用于编辑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>
Run Code Online (Sandbox Code Playgroud)

mzj*_*zjn 15

您可以使用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]:
Run Code Online (Sandbox Code Playgroud)

  • 值得一提的是,它需要`xml`库,而不是`lxml`,如果没有规范运行,beautifulsoup建议这样做。lxml不保持大小写。 (2认同)