python - beautifulsoup 用于分离 &nbsp 和 ;在 html 标签内

标签 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 ;    

最佳答案

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

相关文章:

python - RabbitMQ 消息丢失

python - 美丽汤 : Insert a line break with soup. get_text

python - Web抓取python : IndexError: list index out of range

python - Python 中 'wb' 文件模式下的 FileNotFoundError?

Python 使用 content-type=x-www-form-urlencoded 请求 POST json 数据

python - Python中整数的字节顺序

python - 如何使用 ImageGrid 向颜色栏添加标签?

Python 拆分数组使用 For 循环来表示每个拆分并将其重新组合在一起

Python - Beautiful Soup 查找文本不起作用

python - re.compile 如何在 BeautifulSoup 中执行 find_all 函数?