📌  相关文章
📜  如何将新标签插入到 BeautifulSoup 对象中?

📅  最后修改于: 2022-05-13 01:55:45.371000             🧑  作者: Mango

如何将新标签插入到 BeautifulSoup 对象中?

在本文中,我们将看到如何将新标签插入到 BeautifulSoup 对象中。请参阅以下示例以更好地了解该主题。

例子:

所需模块:

BeautifulSoup (bs4):是一个Python库,用于从 HTML 和 XML 文件中提取数据。这个模块没有内置于Python。在终端中运行以下命令来安装这个库——

pip install bs4
or
pip install beautifulsoup4

使用 new_tag() 方法创建一个新标签:

可以通过调用 BeautifulSoup 的内置函数new_tag()创建一个新标签。

使用 append() 方法插入一个新标签:



新标签被附加到父标签的末尾。

Python3
# Import Module
from bs4 import BeautifulSoup
  
# HTML Document
HTML_DOC = """
              
               
                    Add new Tag 
               
               
                    
This is sample div 1
                    
This is sample div 2
                                          """    # Function to append new tag def addTag(html):        # parse html content     soup = BeautifulSoup(html, "html.parser")        # create new tag     # Here we are creating a new div     new_div = soup.new_tag("div")        # Adding content to div     new_div.string = " This is new div "        # Appending new div to html tree     soup.html.body.append(new_div)        # Printing the modified object     print(soup)       # Function Call addTag(HTML_DOC)


Python3
# Import Module
from bs4 import BeautifulSoup
  
# HTML Document
HTML_DOC = """
              
               
                    Add new Tag 
               
               
                    
This is sample div 1
                    
This is sample div 2
                                          """    # Function to inset new tag def addTag(html):        # parse html content     soup = BeautifulSoup(html, "html.parser")        # create new tag     # Here we are creating a new div     new_div = soup.new_tag("div")        # Adding content to div     new_div.string = " This is new div "        # Inserting new div to html tree     # Here, 2 represents the position     # where we want to insert the new tag     soup.html.body.insert(2, new_div)        # Printing the modified object     print(soup)       # Function Call addTag(HTML_DOC)


Python3
# Import Module
from bs4 import BeautifulSoup
  
# HTML Document
HTML_DOC = """
              
               
                    Add new Tag 
               
               
                    
This is sample div 1
                    
This is sample div 2
                                          """    # Function to insert new tag before given tag def addTag(html):        # parse html content     soup = BeautifulSoup(html, "html.parser")        # create new tag     # Here we are creating a new div     new_div_before = soup.new_tag("div")        # Adding content to div     new_div_before.string = " This is new div before div 1 "        # Inserting new tag before div 1     soup.html.body.div.insert_before(new_div_before)        # Printing the modified object     print(soup)       # Function Call addTag(HTML_DOC)


Python3
# Import Module
from bs4 import BeautifulSoup
  
# HTML Document
HTML_DOC = """
              
               
                    Add new Tag 
               
               
                    
This is sample div 1
                    
This is sample div 2
                                          """    # Function to insert new tag after given tag def addTag(html):        # parse html content     soup = BeautifulSoup(html, "html.parser")        # create new tag     # Here we are creating a new div     new_div_after = soup.new_tag("div")        # Adding content to div     new_div_after.string = " This is new div after div 1 "        # Inserting new tag after div 1     soup.html.body.div.insert_after(new_div_after)        # Printing the modified object     print(soup)       # Function Call addTag(HTML_DOC)


输出:

使用 insert() 方法插入新标签:

使用此方法,新标签不会附加到父标签的末尾,而是插入到给定的数字位置。它的工作原理与Python列表的 .insert() 方法相同。例如,如果我们想在 div 1 和 div 2 之间插入新的 div,我们可以使用

soup.html.body.insert(2, new_div)

这将在位置 2 处插入新的 div,即,在旧的 2 个 div 之间。

蟒蛇3

# Import Module
from bs4 import BeautifulSoup
  
# HTML Document
HTML_DOC = """
              
               
                    Add new Tag 
               
               
                    
This is sample div 1
                    
This is sample div 2
                                          """    # Function to inset new tag def addTag(html):        # parse html content     soup = BeautifulSoup(html, "html.parser")        # create new tag     # Here we are creating a new div     new_div = soup.new_tag("div")        # Adding content to div     new_div.string = " This is new div "        # Inserting new div to html tree     # Here, 2 represents the position     # where we want to insert the new tag     soup.html.body.insert(2, new_div)        # Printing the modified object     print(soup)       # Function Call addTag(HTML_DOC)

输出:



使用 insert_before() 方法插入新标签:

insert_before() 方法用于在给定标签之前插入一个新标签。

蟒蛇3

# Import Module
from bs4 import BeautifulSoup
  
# HTML Document
HTML_DOC = """
              
               
                    Add new Tag 
               
               
                    
This is sample div 1
                    
This is sample div 2
                                          """    # Function to insert new tag before given tag def addTag(html):        # parse html content     soup = BeautifulSoup(html, "html.parser")        # create new tag     # Here we are creating a new div     new_div_before = soup.new_tag("div")        # Adding content to div     new_div_before.string = " This is new div before div 1 "        # Inserting new tag before div 1     soup.html.body.div.insert_before(new_div_before)        # Printing the modified object     print(soup)       # Function Call addTag(HTML_DOC)

输出:

使用 insert_after() 方法插入新标签:

insert_after() 方法用于在给定标签之后插入一个新标签。

蟒蛇3

# Import Module
from bs4 import BeautifulSoup
  
# HTML Document
HTML_DOC = """
              
               
                    Add new Tag 
               
               
                    
This is sample div 1
                    
This is sample div 2
                                          """    # Function to insert new tag after given tag def addTag(html):        # parse html content     soup = BeautifulSoup(html, "html.parser")        # create new tag     # Here we are creating a new div     new_div_after = soup.new_tag("div")        # Adding content to div     new_div_after.string = " This is new div after div 1 "        # Inserting new tag after div 1     soup.html.body.div.insert_after(new_div_after)        # Printing the modified object     print(soup)       # Function Call addTag(HTML_DOC)

输出: