使用 BeautifulSoup 创建带有嵌套标签的新标签

我们如何使用 BeutifulSoup 创建带有嵌套标签的新标签?


例如,给定以下 HTML:


html = """

   <div id="root">

   </div>

"""

例如,所需的输出是:


html = """

   <div id="root">

      <div id="child">

         <div id="grandchild">

         </div>

      </div>

   </div>

"""


天涯尽头无女友
浏览 64回答 2
2回答

蛊毒传说

这是一个相当复杂的代码,但这就是它可以完成的方法:from bs4 import BeautifulSouphtml = """&nbsp; &nbsp;<div id="root">&nbsp; &nbsp;</div>"""# parse the rootroot = BeautifulSoup(html)# create the childchild = BeautifulSoup('<div id="child" />')# create the grandchild under the child, and append grandchild to childgrandchild = child.new_tag('div', attrs={'id': 'grandchild'})child.div.append(grandchild)&nbsp;# create the child under the root, and append child to rootroot.new_tag(child.html.contents[0].div)root.div.append(child.html.contents[0].div)注意:如果你打印root:&nbsp;[...]&nbsp;print(root.prettify())&nbsp;输出是:&nbsp;<html>&nbsp; &nbsp;<body>&nbsp; &nbsp; &nbsp; <div id="root">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div id="child">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div id="grandchild">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp;</body>&nbsp;</html>这意味着root现在是一个完整的 HTML 文档。因此,如果您想用作rootdiv,请确保使用root.div.最后一行 ( root.div.append) 为空child,因此如果在执行最后一行后打印它:[...]print(child.prettify())&nbsp;输出是:<html>&nbsp; <body>&nbsp; </body></html>

慕码人2483693

您可以将另一个附加soup到标签中。例如:from bs4 import BeautifulSouphtml = """&nbsp; &nbsp;<div id="root">&nbsp; &nbsp;</div>"""to_append = '''&nbsp; <div id="child">&nbsp; &nbsp; &nbsp;<div id="grandchild">&nbsp; &nbsp; &nbsp;</div>&nbsp; </div>'''soup = BeautifulSoup(html, 'html.parser')soup.select_one('div#root').append(BeautifulSoup(to_append, 'html.parser'))print(soup.prettify())印刷:<div id="root">&nbsp;<div id="child">&nbsp; <div id="grandchild">&nbsp; </div>&nbsp;</div></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python