Skip to main content
 首页 » 编程设计

python之替换 beautifulsoup 中的 p> 和 br> 标签

2024年10月24日9powertoolsteam

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

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 

请您参考如下方法:

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