Printing Only Outer Tags In Html Code Using Beautifulsoup
Part of whole HTML code looks as follows
Solution 1:
You could set the string
attribute to an empty string (''
):
html = """
<td class="col2">
<a class="reserve" data-target="#myModal" data-toggle="modal"
href="example.com" rel="nofollow"></a></td>
"""
soup = BeautifulSoup(html)
x = soup.find('td', class_='col2')
x.string = ''print(x)
Output
<tdclass="col2"></td>
Here is what documentation tells about it:
If you set a tag's
.string
attribute, the tag's contents are replaced with the string you give
Be careful: if the tag contained other tags, they and all their contents will be destroyed.
Solution 2:
You can extract all elements inside td.col2
with extract()
function:
data = '''
<td class="col2">
<a class="reserve" data-target="#myModal" data-toggle="modal"
href="example.com" rel="nofollow"></a></td>'''from bs4 import BeautifulSoup
soup = BeautifulSoup(data, 'lxml')
for td in soup.select('td.col2'):
for t in td.select('*'):
t.extract()
print(td)
Prints:
<tdclass="col2"></td>
Post a Comment for "Printing Only Outer Tags In Html Code Using Beautifulsoup"