使用美丽的汤保留&#bsp; 实体

Hol*_*rel 10 python beautifulsoup html-parsing html-entities web-scraping

我想从网上刮一张桌子,并保持  实体完好无损,以便我以后可以重新发布为HTML.尽管如此,BeautifulSoup似乎正在将这些转换为空间.例:

from bs4 import BeautifulSoup

html = "<html><body><table><tr>"
html += "<td>&nbsp;hello&nbsp;</td>"
html += "</tr></table></body></html>"

soup = BeautifulSoup(html)
table = soup.find_all('table')[0]
row = table.find_all('tr')[0]
cell = row.find_all('td')[0]

print cell
Run Code Online (Sandbox Code Playgroud)

观察结果:

<td> hello </td>
Run Code Online (Sandbox Code Playgroud)

要求的结果:

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

ale*_*cxe 7

在bs4中convertEntities,不再支持BeautifulSoup构造函数的参数.HTML实体始终转换为相应的Unicode字符(请参阅docs).

根据文档,您需要使用输出格式化程序,如下所示:

print soup.find_all('td')[0].prettify(formatter="html")
Run Code Online (Sandbox Code Playgroud)