更换beautifulsoup中的<p>和<br/>标签

时间:2015-12-07 15:35:38

标签: python beautifulsoup

您好我正在寻找一种用空格替换代码中标签的方法。

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

1 个答案:

答案 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