我试图从此HTML标记中获取所有文本,我将其存储在变量tag
中:
<td rowspan="2" style="text-align: center;"><a href="/wiki/Glenn_Miller" title="Glenn Miller">Glenn Miller</a> & His Orchestra</td>
结果应为"Glenn Miller & His Orchestra"
。
但print
ing tag.find(text=True)
会返回此信息:"Glenn Miller"
。
如何在td
元素中获取其余文本?
答案 0 :(得分:4)
tag.find(text=True)
将返回第一个匹配的文本节点。请改用.get_text()
:
>>> from bs4 import BeautifulSoup
>>> data = '<td rowspan="2" style="text-align: center;"><a href="/wiki/Glenn_Miller" title="Glenn Miller">Glenn Miller</a> & His Orchestra</td>'
>>> soup = BeautifulSoup(data, "html.parser")
>>> tag = soup.td
>>> tag.get_text()
'Glenn Miller & His Orchestra'