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

时间:2022-10-16 07:20:28

I am writing a script that edits an XML file with BeautifulStoneSoup, but the library converts all tags to lower case. Is there an option to conserve the case?

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

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>

1 个解决方案

#1


15  

You could use Beautiful Soup 4, as follows (requires the lxml XML library):

您可以使用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]:

#1


15  

You could use Beautiful Soup 4, as follows (requires the lxml XML library):

您可以使用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]: