2013-10-07 45 views
3
from bs4 import BeautifulSoup 

xmlcontent = "some text with <tags>" 

bs = BeautifulSoup(xmlcontent, "xml") 

print bs 

输出使用 “XML” 时 如何删除美丽的汤

<?xml version="1.0" encoding="utf-8"?> 
some text with <tags> 

是否有可能不输出:

<?xml version="1.0" encoding="utf-8"?> 

我知道,如果使用lxml,删除添加的<body>标签我可以这样做:

bs = BeautifulSoup(xmlcontent, "lxml") 

print bs.body.next 

是否有与xml一同使用的等价物,以便不包含xml版本和编码?

我选择使用xmllxml的内容被解析将最经常是XML格式 - 这是最好的选择还是我可以只使用lxml XML内容?

回答

-1

这似乎工作:

from bs4 import BeautifulSoup 

xmlcontent = "some text with <tags>" 

bs = BeautifulSoup(xmlcontent, "xml") 

bs = bs.encode_contents() 

print type(bs) # it's a string 

print bs 

# some text with <tags>