Beautiful Soup的强项是文档树的搜索,但同时也可以方便的修改文档树
修改tag的名称和属性
>>> soup = BeautifulSoup('Extremely bold')
>>> tag = soup.b
>>> tag.name = "blockquote"
>>> tag["class"] = "verybold"
>>> tag["id"] = 1
>>> tag
Extremely bold
>>> del tag["class"]
>>> del tag["id"]
>>> tag
Extremely bold
修改 .string
给tag的 .string 属性赋值,就相当于用当前的内容替代了原来的内容:
>>> markup = 'I linked to example.com'
>>> soup = BeautifulSoup(markup)
>>> tag = soup.a
>>> tag.string = "New link text."
>>> tag
New link text.
注意: 如果当前的tag包含了其它tag,那么给它的 .string 属性赋值会覆盖掉原有的所有内容包括子tag
append()
Tag.append() 方法想tag中添加内容,就好像Python的列表的 .append() 方法:
>>> soup = BeautifulSoup("Foo")
>>> soup.a.append("Bar")
>>> soup
FooBar
>>> soup.a.contents
['Foo', 'Bar']
NavigableString()和 .new_tag()
如果想添加一段文本内容到文档中也没问题,可以调用Python的 append() 方法 或调用 NavigableString 的构造方法:
soup = BeautifulSoup("")
tag = soup.b
tag.append("Hello")
new_string = NavigableString(" there")
tag.append(new_string)
tag
# Hello there.
tag.contents
# [u'Hello', u' there']
注意:这里我输入时报错,好像 NavigableString未生效。
如果想要创建一段注释,或 NavigableString 的任何子类, 只要调用 NavigableString 的构造方法:
>>> from bs4 import Comment
>>> new_comment = soup.new_string("Nice to see you.", Comment)
>>> tag.append(new_comment)
>>> tag
Hello
创建一个tag最好的方法是调用工厂方法 BeautifulSoup.new_tag() :
>>> soup = BeautifulSoup("")
>>> original_tag = soup.b
>>> new_tag = soup.new_tag("a",href="http://www.example.com")
>>> original_tag.append(new_tag)
>>> original_tag
>>> new_tag.string = "Link text."
>>> original_tag
Link text.
注意:第一个参数作为tag的name,是必填,其它参数选填
insert()
Tag.insert() 方法与 Tag.append() 方法类似,区别是不会把新元素添加到父节点 .contents 属性的最后,而是把元素插入到指定的位置.与Python列表总的 .insert() 方法的用法下同:
>>> markup = 'I linked to example.com'
>>> soup = BeautifulSoup(markup)
>>> tag = soup.a
>>> tag.insert(1,"but did not endorse ")
>>> tag
I linked to but did not endorse example.com
>>> tag.contents
['I linked to ', 'but did not endorse ', example.com]
insert_before() 和 insert_after()
insert_before() 方法在当前tag或文本节点前插入内容:
>>> soup = BeautifulSoup("stop")
>>> tag = soup.new_tag("i")
>>> tag.string = "Don't"
>>> soup.b.string.insert_before(tag)
>>> soup.b
Don'tstop
insert_after() 方法在当前tag或文本节点后插入内容:
>>> soup.b.i.insert_after(soup.new_string(" ever "))
>>> soup.b
Don't ever stop
>>> soup.b.contents
[Don't, ' ever ', 'stop']
clear()
Tag.clear() 方法移除当前tag的内容:
>>> markup = 'I linked to example.com'
>>> soup = BeautifulSoup(markup)
>>> tag = soup.a
>>> tag.clear()
>>> tag
extract()
PageElement.extract()方法将当前tag移除文档树,并作为方法结果返回:
>>> markup = 'I linked to example.com'
>>> soup = BeautifulSoup(markup)
>>> a_tag = soup.a
>>> i_tag = soup.i.extract()
>>> a_tag
I linked to
>>> i_tag
example.com
>>> print(i_tag.parent)
None
这个方法实际上产生了2个文档树: 一个是用来解析原始文档的 BeautifulSoup 对象,另一个是被移除并且返回的tag.被移除并返回的tag可以继续调用 extract 方法:
>>> my_string = i_tag.string.extract()
>>> my_string
'example.com'
>>> print(my_string.parent)
None
>>> i_tag
decompose()
Tag.decompose() 方法将当前节点移除文档树并完全销毁:
>>> markup = 'I linked to example.com'
>>> soup = BeautifulSoup(markup)
>>> a_tag = soup.a
>>> soup.i.decompose()
>>> a_tag
I linked to
replace_with()
PageElement.replace_with() 方法移除文档树中的某段内容,并用新tag或文本节点替代它:
>>> markup = 'I linked to example.com'
>>> soup = BeautifulSoup(markup)
>>> a_tag = soup.a
>>> new_tag = soup.new_tag("b")
>>> new_tag.string ="example.net"
>>> a_tag.i.replace_with(new_tag)
example.com
>>> a_tag
I linked to example.net
replace_with() 方法返回被替代的tag或文本节点,可以用来浏览或添加到文档树其它地方
wrap()
PageElement.wrap() 方法可以对指定的tag元素进行包装 ,并返回包装后的结果:
>>> soup = BeautifulSoup("I wish I was bold.
")
>>> soup.p.string.wrap(soup.new_tag("b"))
I wish I was bold.
>>> soup.p.wrap(soup.new_tag("div"))
I wish I was bold.
unwrap()
Tag.unwrap() 方法与 wrap() 方法相反.将移除tag内的所有tag标签,该方法常被用来进行标记的解包:
>>> markup = 'I linked to example.com'
>>> soup = BeautifulSoup(markup)
>>> a_tag = soup.a
>>> a_tag.i.unwrap()
>>> a_tag
I linked to example.com
与 replace_with() 方法相同, unwrap() 方法返回被移除的tag