Issue
How can we create a new tag with nested tags by using BeutifulSoup?
For example, given the following HTML:
html = """
<div id="root">
</div>
"""
The desired output would be, for example:
html = """
<div id="root">
<div id="child">
<div id="grandchild">
</div>
</div>
</div>
"""
Solution
It's quite an involved code, but that's how it can be done:
from bs4 import BeautifulSoup
html = """
<div id="root">
</div>
"""
# parse the root
root = BeautifulSoup(html)
# create the child
child = BeautifulSoup('<div id="child" />')
# create the grandchild under the child, and append grandchild to child
grandchild = child.new_tag('div', attrs={'id': 'grandchild'})
child.div.append(grandchild)
# create the child under the root, and append child to root
root.new_tag(child.html.contents[0].div)
root.div.append(child.html.contents[0].div)
Note that:
if you print
root
:[...] print(root.prettify())
the output is:
<html> <body> <div id="root"> <div id="child"> <div id="grandchild"> </div> </div> </div> </body> </html>
meaning that
root
is now a full HTML document.
So if you want to useroot
as a div, make sure you access it as such, usingroot.div
.The last line (
root.div.append
) emptieschild
, so that if you print it after executing that last line:[...] print(child.prettify())
the output is:
<html> <body> </body> </html>
Answered By - OfirD
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.