提取元素并插入空格

lum*_*ere 18 python beautifulsoup html-parsing

我在python中使用BeautifulSoup解析html

我不知道如何在提取文本元素时插入空格

这是代码:

import BeautifulSoup
soup=BeautifulSoup.BeautifulSoup('<html>this<b>is</b>example</html>')
print soup.text
Run Code Online (Sandbox Code Playgroud)

那么输出就是

thisisexample

但我想为此插入一个空格

是例子

我该如何插入空格?

mou*_*uad 40

getText改为使用:

import BeautifulSoup
soup=BeautifulSoup.BeautifulSoup('<html>this<b>is</b>example</html>')

print soup.getText(separator=u' ')
# u'this is example'
Run Code Online (Sandbox Code Playgroud)

  • 这是最近版本的 bs4/python3 中的 `soup.get_text(separator='')`,但传递分隔符关键字参数的工作方式相同。 (2认同)