extracting element and insert a space

前端 未结 3 930
挽巷
挽巷 2021-02-04 03:29

im parsing html using BeautifulSoup in python

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

this is the code:

import BeautifulSo         


        
相关标签:
3条回答
  • 2021-02-04 03:48

    One may want to use also with strip argument

    bs = BeautifulSoup("<html>this<b>is  </b>example</html>")
    print(bs.get_text())  # thisis  example
    print(bs.get_text(separator=" "))  # this is   example
    print(bs.get_text(separator=" ", strip=True))  # this is example
    
    0 讨论(0)
  • 2021-02-04 03:59

    Use getText instead:

    import BeautifulSoup
    soup=BeautifulSoup.BeautifulSoup('<html>this<b>is</b>example</html>')
    
    print soup.getText(separator=u' ')
    # u'this is example'
    
    0 讨论(0)
  • 2021-02-04 04:02

    If your version of Beautifulsoup does not have getText then you could do this:

    In [26]: ' '.join(soup.findAll(text=True))
    Out[26]: u'this is example'
    
    0 讨论(0)
提交回复
热议问题