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

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

关于python - 替换beautifulsoup中的<p>和<br>标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34137370/

10-15 07:46