如何在BeautifulSoup.contents中保留空格

我在网上找到的大多数示例都显示了如何删除空格-但就我而言,我需要保留它。

html = "I can flip this whole thing with one hand\n               <span>D#m</span>\nThe ringleader man\n<span>A#</span>                           <span>Dm</span>                          <span>A#</span>\nI know~~~~ it's a fact that you'd rather just have some of me instead"

bs = BeautifulSoup(html, 'html.parser')

content = (unicode('').join(unicode(content) for content in bs.contents))

我希望保留空白(“ html”变量包含pre标记的内容),但是似乎用一个空格代替了多个空格。

如何保存/获取给定beautifulsoup解析器的原始内容?

回答:

如果您要解析的内容在

标记中,则html解析器似乎仅保留空白-在我的情况下,pre标记已删除。新增中

html = "<pre>" + html + "</pre>"

保留空白。

以上是 如何在BeautifulSoup.contents中保留空格 的全部内容, 来源链接: utcz.com/qa/398068.html

回到顶部