Search code examples
pythonhtml-parsingbeautifulsoup

extracting element and insert a space


im parsing html using BeautifulSoup in python

i dont know how to insert a space when extracting text element

this is the code:

import BeautifulSoup
soup=BeautifulSoup.BeautifulSoup('<html>this<b>is</b>example</html>')
print soup.text

then output is

thisisexample

but i want to insert a space to this like

yes is example

how do i insert a space?


Solution

  • Use getText instead:

    import BeautifulSoup
    soup=BeautifulSoup.BeautifulSoup('<html>this<b>is</b>example</html>')
    
    print soup.getText(separator=u' ')
    # u'this is example'