如何在beautifulsoup(不仅是文本,还有html标签等/Python)中让每一句话都用标签括起来



我想从下面的样本HTML中获得Orange1p

我可以得到像Orange1p这样的集合句,但不能单独得到两个句子。

有办法得到两个句子吗?

样品HTML:

<td class="info">Orange<br/>1p</td>

当前使用的代码:

soup = BeautifulSoup(html_doc, 'html.parser')
data = soup.find("td", {"class": "info"}) # with current output of `Orange1p`
from bs4 import BeautifulSoup
html_doc = """<td class="info">Orange<br/>1p</td>"""
soup = BeautifulSoup(html_doc, 'html.parser')
print(list(soup.find("td", {'class': 'info'}).strings))

输出:

['Orange', '1p']

最新更新