Extract content within a tag with BeautifulSoup

The contents operator works well for extracting text from <tag>text</tag> .


<td>My home address</td> example:

s="<td>My home address</td>"
soup =  BeautifulSoup(s)
td = soup.find('td') #<td>My home address</td>
td.contents #My home address

<td><b>Address:</b></td> example:

s="<td><b>Address:</b></td>"
soup =  BeautifulSoup(s)
td = soup.find('td').find('b') #<b>Address:</b>
td.contents #Address:

Leave a Comment