分开的美丽汤;在html标签中

时间:2018-07-31 01:10:15

标签: python beautifulsoup

我的代码

html = "<td>1.08&nbsp; 8.00&nbsp; 151.00</td>"
from bs4 import BeautifulSoup

print BeautifulSoup(html,"lxml").renderContents()

输出:

<html><body><td>1.08  8.00  151.00</td></body></html>

所需的输出:

1.08 ; 8.00 ; 151.00 ;    

1 个答案:

答案 0 :(得分:4)

>>> from bs4 import BeautifulSoup
... html = "<td>1.08&nbsp; 8.00&nbsp; 151.00</td>"
... soup = BeautifulSoup(html, "lxml")
>>> print(soup.find('td').text)
1.08  8.00  151.00
>>> nums = soup.find('td').text.split()
>>> nums
['1.08', '8.00', '151.00']
>>> ' ; '.join(nums)
'1.08 ; 8.00 ; 151.00'