BeautifulSoup和 

Win*_*ert 2 html python beautifulsoup

我的代码:

html = "<tag>&nbsp;</tag>"
from bs4 import BeautifulSoup
print BeautifulSoup(html).renderContents()
Run Code Online (Sandbox Code Playgroud)

输出:

<tag>?á</tag>
Run Code Online (Sandbox Code Playgroud)

期望的输出:

<tag>&nbsp;</tag>
Run Code Online (Sandbox Code Playgroud)

BeautifulSoup似乎被替换为我的破解空间html转义与unicode字符意味着同样的事情.但这并没有完全通过我的系统,最终成为一个不间断的空间,从而没有做我想要的.有没有办法告诉BeautifulSoup不这样做?

mat*_*ata 6

使用encode_contents而不是renderContents,encodeprettify.他们都支持这个formatter论点,并'html'作为格式化程序传递:

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

生产:

<tag>&nbsp;</tag>
Run Code Online (Sandbox Code Playgroud)