How do I get all text from within this tag?

我是研究僧i 提交于 2021-02-07 22:39:18

问题


I'm trying to get all text from within this HTML tag, which I store in variable tag:

<td rowspan="2" style="text-align: center;"><a href="/wiki/Glenn_Miller" title="Glenn Miller">Glenn Miller</a> &amp; His Orchestra</td>

The result should be "Glenn Miller & His Orchestra".

But printing tag.find(text=True) returns this: "Glenn Miller".

How do I get the rest of the text within the td element?


回答1:


tag.find(text=True) would return the first matching text node. Use .get_text() instead:

>>> from bs4 import BeautifulSoup
>>> data = '<td rowspan="2" style="text-align: center;"><a href="/wiki/Glenn_Miller" title="Glenn Miller">Glenn Miller</a> &amp; His Orchestra</td>'
>>> soup = BeautifulSoup(data, "html.parser")
>>> tag = soup.td
>>> tag.get_text()
'Glenn Miller & His Orchestra'


来源:https://stackoverflow.com/questions/37336326/how-do-i-get-all-text-from-within-this-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!