Python 如何在BeautifulSoup.BeautifulStoneSoup中维护区分大小写的标记?
我正在编写一个脚本,用Python 如何在BeautifulSoup.BeautifulStoneSoup中维护区分大小写的标记?,python,xml,beautifulsoup,Python,Xml,Beautifulsoup,我正在编写一个脚本,用BeautifulStoneSoup编辑一个XML文件,但库会将所有标记转换为小写。是否有保留案例的选项 import BeautifulSoup xml = "<TestTag>a string</TestTag>" soup = BeautifulSoup.BeautifulStoneSoup(xml, markupMassage=False) print soup.prettify() # or soup.renderC
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>
导入美化组
xml=“字符串”
soup=BeautifulSoup.BeautifulStoneSoup(xml,markup按摩=False)
打印soup.prettify()#或soup.renderContents()
#印刷品
>>>一串
#而不是预期的
>>>一串
您可以使用,如下所示(需要lxml库):
[10]中的:来自bs4导入BeautifulSoup
在[11]中:xml=“字符串”
在[12]中:soup=beautifulsou(xml,“xml”)
在[13]中:打印汤
一串
在[14]中:
谢谢,升级成功,效果很好。对于未来的读者:运行pip install BeautifulSoup4
notpip install BeautifulSoup4--upgrade
值得一提的是,它需要xml
库,而不是lxml
,如果在没有规范的情况下运行,这是beautifulsop建议的lxml
不维护大小写。@KeithSmiley:是的,当使用soup=BeautifulSoup(xml,“lxml”)
时,使用lxml的HTML解析器。请参阅。我在Win7
上安装了BeautifulSoup4
,但它没有附带lxml
。以下是我在安装时遇到的问题:
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]: