space-haven/practice/replacetest.py

46 lines
741 B
Python
Raw Normal View History

from bs4 import BeautifulSoup
markup = """
<!DOCTYPE>
<html>
<head><title>Example</title></head>
<body>
<div id="parent">
<p>
This is child of div with id = "parent".
<span>Child of "P"</span>
</p>
<div>
Another Child of div with id = "parent".
</div>
</div>
<p>
Piyush
</p>
</body>
</html>
"""
# parsering string to HTML
soup = BeautifulSoup(markup, 'html.parser')
# finding tag whose child to be deleted
div_bs4 = soup.find('div')
print(div_bs4)
print('-------')
# delete the child element
div_bs4.clear()
new_tag = soup.new_tag('s', attrs={'sk' : 2, 'level' : 5})
div_bs4.append(new_tag)
print(div_bs4)