2013-06-20 5 views
2

내 코드 :BeautifulSoup로하고  

html = "<tag>&nbsp;</tag>" 
from bs4 import BeautifulSoup 
print BeautifulSoup(html).renderContents() 

출력 :

<tag> </tag> 

원하는 출력 :

<tag>&nbsp;</tag> 

BeautifulSoup로는 유니 코드 문자로 내 깨는 공간 HTML 탈출을 대체 할 것으로 보인다 그것은 같은 것을 의미합니다. 그러나 그것이 내 시스템을 통해 모든 것을 만들지는 않으며 끝내지 않는 공간이되어 결국 내가 원하는 것을하지 않게된다. BeautifulSoup에게 그렇게하지 말라고 말하는 방법이 있습니까?

답변

5

renderContents 또는 encode 또는 prettify 대신 encode_contents을 사용하십시오. 그들은 모두 formatter 인수를 지원하고 포맷으로 'html' 전달합니다

html = "<tag>&nbsp;</tag>" 
from bs4 import BeautifulSoup 
print BeautifulSoup(html).encode_contents(formatter='html') 

가 생성됩니다

<tag>&nbsp;</tag> 
관련 문제