How to add space around removed tags in BeautifulSoup
One option would be to find all text nodes and join them with a space:
" ".join(item.strip() for item in poems.find_all(text=True))
Additionally, you are using beautifulsoup3
package which is outdated and not maintained. Upgrade to beautifulsoup4
:
pip install beautifulsoup4
and replace:
from BeautifulSoup import BeautifulSoup
with:
from bs4 import BeautifulSoup
get_text()
in beautifoulsoup4
has an optional input called separator
. You can use it as follows :
soup = BeautifulSoup(html)
text = soup.get_text(separator=' ')