我想从网上抓取一张表格并保留 实体完好无损,以便我以后可以重新发布为 HTML。 BeautifulSoup 似乎正在将这些转换为空格。示例:
from bs4 import BeautifulSoup
html = "<html><body><table><tr>"
html += "<td> hello </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> hello </td>
最佳答案
在 bs4 中,不再支持 BeautifulSoup 构造函数的 convertEntities
参数。 HTML 实体总是被转换成相应的 Unicode 字符(参见 docs )。
根据文档,您需要使用输出格式化程序,如下所示:
print soup.find_all('td')[0].prettify(formatter="html")
关于python - 使用 Beautiful Soup 保存实体进行抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16135951/