gpt4 book ai didi

python - 如何使用 BeautifulSoup 4 替换或删除 HTML 实体,如 " "

转载 作者:IT老高 更新时间:2023-10-28 21:07:22 25 4
gpt4 key购买 nike

我正在使用 Python 和 BeautifulSoup 4 库处理 HTML,但我找不到用空格替换   的明显方法。相反,它似乎被转换为 Unicode 不间断空格字符。

我是否遗漏了一些明显的东西?   最好的替换方法是什么?使用 BeautifulSoup 的普通空间?

编辑添加我使用的是最新版本 BeautifulSoup 4,因此 Beautiful Soup 3 中的 convertEntities=BeautifulSoup.HTML_ENTITIES 选项不可用。

最佳答案

>>> soup = BeautifulSoup('<div>a&nbsp;b</div>')
>>> soup.prettify(formatter=lambda s: s.replace(u'\xa0', ' '))
u'<html>\n <body>\n <div>\n a b\n </div>\n </body>\n</html>'

关于python - 如何使用 BeautifulSoup 4 替换或删除 HTML 实体,如 "&nbsp;",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15138406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com