您好我正在寻找一种用空格替换代码中标签的方法。
soup = BeautifulSoup("<p>Something</p><p>Something</p>")
print soup.get_text()
SomethingSomething
#When I do get_text now I would get SomethingSomething but I want Something Something
答案 0 :(得分:2)
get_text
功能允许您指定单个元素的文本分隔的内容:
In [1]: from bs4 import BeautifulSoup
In [2]: soup = BeautifulSoup("<p>Something</p><p>Something</p>")
In [3]: print soup.get_text(separator=u' ')
Something Something
参考:http://www.crummy.com/software/BeautifulSoup/bs4/doc/#get-text