首页 > 解决方案 > 如何在beatifulsoup中的标签前添加空格

问题描述

我有以下代码:

html = urlopen(req).read()
soup = BeautifulSoup(html, "lxml")
# remove all script and style elements
for script in soup(["script", "style"]):
    script.extract()
# get text
text = soup.get_text()

问题是,如果在我的 html 页面中有类似的内容 Oxford<br />Laboratory,并且在删除样式后,我得到OxfordLaboratory

所以这是我的问题:我怎样才能在所有之前添加一个空格,<这样单词就不会组合在一起?

标签: pythonbeautifulsoup

解决方案


正如文档所述:

您可以指定一个字符串用于将文本位连接在一起:

# soup.get_text("|")

在您的情况下,您需要一个空格 ( " ") 作为分隔符。


推荐阅读