相关疑难解决方法(0)

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

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

python beautifulsoup html-parsing html-entities web-scraping

10
推荐指数
1
解决办法
2378
查看次数