extracting element and insert a space

為{幸葍}努か 提交于 2019-12-20 10:31:36

问题


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?


回答1:


Use getText instead:

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

print soup.getText(separator=u' ')
# u'this is example'



回答2:


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'


来源:https://stackoverflow.com/questions/6467043/extracting-element-and-insert-a-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!