python 第三方库BeautifulSoup4文档学习(5)

bs4 修改文档树

BeautifulSoup还能对文档树进行修改操作,下面主要是列举一些修改文档书的方法

下面的例子都已引入相应的库或包

from bs4 import BeautifulSoup,Comment
from bs4.element import NavigableString

修改tag的名称和属性

soup = BeautifulSoup('<a id="a1">这是一个假链接</a>',features="html.parser")
tag = soup.a
tag.name = "p"
tag['class'] = "pharagraph1"
tag['id'] = "p1"
print(tag)

# output: <p class="pharagraph1" id="p1">这是一个假链接</p>

.string属性

.string可以覆盖原有的tag中的文本,如果tag中包含有子tag的,也会被替换掉

soup = BeautifulSoup('<a id="a1">这是一个假链接</a>',features="html.parser")
tag = soup.a
tag.name = "p"
tag["id"] = "p1"
tag.string = "变成了一个段落"
print(tag)

# output: <p id="p1">变成了一个段落</p>

.append()方法

.append()方法将会在原有的tag中追加内容

soup = BeautifulSoup('<a id="a1">这是一个假链接</a>',features="html.parser")
tag = soup.a
tag.name = "p"
tag["id"] = "p1"
tag.string = "变成了一个段落"
tag.append(" a little boy")
print(tag)

# output: <p id="p1">变成了一个段落 a little boy</p>

# 除了上面使用append直接追加的方式,还可以调用NavigableString的构造方法来追加一段文本
new_stirng = NavigableString(" n string")
tag.append(new_stirng)
# output: <p id="p1">变成了一个段落 a little boy n string</p>

# 调用NavigableString的构造方法可以为tag创建一段注释,或者是添加一个NavigableString的子类
new_comment = soup.new_string("This is a comment",Comment)
tag.append(new_comment)
# output: <p id="p1">变成了一个段落 a little boy n string<!--This is a comment--></p>

创建一个new_tag

创建一个新的tag最好的方法是调用工厂方法BeautifulSoup.new_tag(),填入其中的参数tag的name为必填,其他参数选填

new_tag = BeautifulSoup("<b></b>","html.parser").new_tag("a",href="http://localhost")
soup = BeautifulSoup('<a id="a1">这是一个假链接</a>',features="html.parser")
tag = soup.a
tag.append(new_tag)
new_tag.string = "Just like fire"
print(tag)

# output: <a id="a1">这是一个假链接<a href="http://localhost">Just like fire</a></a>

.insert()方法

tag的insert()方法是在指定的列表索引位置插入元素,索引从0开始

soup = BeautifulSoup('<a id="a1">这是一个假链接</a>',features="html.parser")
tag = soup.a
tag.insert(0," Insert element")
print(tag)

# output: <a id="a1"> Insert element这是一个假链接</a>

.insert_before()与.insert_after()方法

tag的insert_before方法与insert_after方法就是在tag或者文本节点的前或后插入内容

soup = BeautifulSoup('<a id="a1">这是一个假链接</a>',features="html.parser")
soup.a.string.insert_before("test string")
# output: <a id="a1">test string这是一个假链接</a>

# 注意这里上下两句不能同时运行
# soup.a.string.insert_after(soup.new_tag("div",id="div1"))
# output: <a id="a1">这是一个假链接<div id="div1"></div></a>

.clear()方法

clear方法用于清除tag中的内容

soup = BeautifulSoup('<a id="a1">这是一个假链接</a>',features="html.parser")
tag = soup.a
tag.clear()
print(tag)
# output: <a id="a1"></a>

.extract()方法

extract方法会从文档树中移除tag,并且将结果返回,并且返回的结果作为一个新的文档树还可以使用extract方法

soup = BeautifulSoup('<a id="a1">这是一个假链接</a>',features="html.parser")
new_tag = soup.new_tag("p",_class="Orignal paragraph")
soup.a.string.insert_before(new_tag)
a_tag = soup.a
p_tag = soup.p.extract()
p_tag.string = "New paragraph"
p_string = p_tag.string.extract()
print(a_tag)
print(p_tag)
print(p_string)

# output: 
# <a id="a1">这是一个假链接</a>
# <p _class="Orignal paragraph"></p>
# New paragraph

.decompose()方法

decompose方法会从文档树中完全移除当前节点并销毁

soup = BeautifulSoup('<a href="http://localhost">This is a link <p>Test paragraph</p></a>',"html.parser")
soup.p.decompose()
print(soup)
# output: <a href="http://localhost">This is a link </a>

.replace_with()方法

replace_with方法会替换掉该元素中的内容

soup = BeautifulSoup('<a href="http://localhost">This is a link <p>Test paragraph</p></a>',"html.parser")
p_tag = soup.p
p_tag.replace_with(soup.new_tag("div",_class="Normal div"))
print(soup)

# output: <a href="http://localhost">This is a link <div _class="Normal div"></div></a>

.wrap()方法与.unwrap()方法

wrap/unwrap可以对元素进行加包装/解包装

soup = BeautifulSoup('<a href="http://localhost">This is a link <p>Test paragraph</p></a>',"html.parser")
soup.p.string.wrap(soup.new_tag("b"))
p_tag = soup.p
print(soup)

# output: <a href="http://localhost">This is a link <p><b>Test paragraph</b></p></a>
soup = BeautifulSoup('<a href="http://localhost">This is a link <p>Test paragraph</p></a>',"html.parser")
soup.p.string.wrap(soup.new_tag("b"))
soup.p.unwrap()
print(soup)

# output: <a href="http://localhost">This is a link <b>Test paragraph</b></a>
posted @ 2022-01-08 18:18  搁浅的感性  阅读(272)  评论(0编辑  收藏  举报