gpt4 book ai didi

python - beautifulsoup 将   和 ; 分开在 html 的标签内

转载 作者:行者123 更新时间:2023-11-28 22:17:07 24 4
gpt4 key购买 nike

我的代码

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 ;    

最佳答案

>>> 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'

关于python - beautifulsoup 将 &nbsp 和 ; 分开在 html 的标签内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51604174/

24 4 0