Username Username - 6 months ago 13
HTML Question

How do I get all text from within this tag?

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
print
ing
tag.find(text=True)
returns this:
"Glenn Miller"
.

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

Answer

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