插入嵌套标签的兄弟元素

时间:2018-11-15 00:18:11

标签: python html beautifulsoup

如何插入包含嵌套标签的兄弟元素?

我正在尝试使用insert_before函数,但这似乎仅适用于单个标记。 例如,拥有

<html>
  <body>
    <div class= first_class>
    <h1 id=Heder1>Header1</h1>
    </div>
  </body>
</html>

我尝试在每个<div class= first_class>上方插入

 <button class="accordion">
  <div class="preface">
    <i>Text</i>
  </div>
  </button>

我尝试使用以下逻辑,但无法正常工作。该部分未插入。

section_code = BeautifulSoup('<button class="accordion"><div class="preface"><i>Text</i></div></button>', 'lxml')

section = section.html.body.contents[0]

titels = soup.find_all("h1")

for title in titels:
    title.parent.insert_before(section)

如何实现?

所需的输出

<html>
  <body>
    <button class="accordion">
      <div class="preface">
        <i>Text</i>
      </div>
    </button>
    <div class= first_class>
    <h1 id=Heder1>Header1</h1>
    </div>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

您只需要多做一点。如果以str代替bs4.element.Tag插入。该字符串将为html编码

from bs4 import BeautifulSoup

html = """
<html>
  <body>
    <div class= first_class>
    <h1 id=Heder1>Header1</h1>
    </div>
  </body>
</html>

"""

insert = """

<button class="accordion"><div class="preface"><i>Text</i></div></button>
"""
insert_content = BeautifulSoup(insert,"lxml")
soup = BeautifulSoup(html,"lxml")

title = soup.find("div")

title.insert_before(insert_content.find("button"))
print(soup.prettify())

输出

<html>
 <body>
  <button class="accordion">
   <div class="preface">
    <i>
     Text
    </i>
   </div>
  </button>
  <div class="first_class">
   <h1 id="Heder1">
    Header1
   </h1>
  </div>
 </body>
</html>