2012-08-28 28 views
12

Ben BeautifulStoneSoup ile bir XML dosyası düzenler bir senaryo yazıyorum ama kütüphane tüm etiketler harfe dönüştürür. Davayı korumak için bir seçenek var mı?Nasıl BeautifulSoup.BeautifulStoneSoup içinde harfe duyarlı etiketlerin bakımını?

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> 

cevap

15

aşağıdaki (Lxml XML kütüphanesi gerektirir) gibi, Beautiful Soup 4 kullanabilirsiniz:

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

sayesinde yükseltme yaptı ve o inşaat büyük. Gelecekteki okuyucular için: vadede '' o 'xml' kütüphane, hiçbir şartname ile çalıştırırsanız BeautifulSoup tavsiye ettiğinin değil' lxml' gerektirdiği belirtilmelidir değer – TankorSmash

+1

--upgrade' BeautifulSoup yüklemek pip değil BeautifulSoup4' yüklemek pip. 'lxml' davasını korumaz. –

+0

@KeithSmiley: 'çorba kullanırken Evet = BeautifulSoup (xml, "Lxml")', LXML HTML ayrıştırıcı kullanılır. Http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser adresine bakın. – mzjn

İlgili konular