7

我想从网上刮一张桌子,并保持& nbsp;实体完好无损,以便稍后可以重新发布为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 

观察结果:

<td> hello </td> 

所需的结果:

<td>&nbsp;hello&nbsp;</td> 

回答

5

在BS4 convertEntities参数BeautifulSoup构造不再被支承。 HTML实体总是转换为相应的Unicode字符(请参见docs)。

根据文档,你需要使用的输出格式,如:

print soup.find_all('td')[0].prettify(formatter="html") 
+0

感谢您的回答:) – 2013-04-28 03:56:10