# 這是Beautiful Soup 4.2.1 中新增的方法
創建一個tag最好的方法是調用工廠方法 `BeautifulSoup.new_tag()` :
```
soup = BeautifulSoup("<b></b>")
original_tag = soup.b
new_tag = soup.new_tag("a", href="http://www.example.com")
original_tag.append(new_tag)
original_tag
# <b><a href="http://www.example.com"></a></b>
new_tag.string = "Link text."
original_tag
# <b><a href="http://www.example.com">Link text.</a></b>
```
第一個參數作為tag的name,是必填,其它參數選填
## insert()
`Tag.insert()` 方法與 `Tag.append()` 方法類似,區別是不會把新元素添加到父節點 `.contents` 屬性的最后,而是把元素插入到指定的位置.與Python列表總的 `.insert()` 方法的用法下同:
```
markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
soup = BeautifulSoup(markup)
tag = soup.a
tag.insert(1, "but did not endorse ")
tag
# <a href="http://example.com/">I linked to but did not endorse <i>example.com</i></a>
tag.contents
# [u'I linked to ', u'but did not endorse', <i>example.com</i>]
```
## insert_before() 和 insert_after()
`insert_before()` 方法在當前tag或文本節點前插入內容:
```
soup = BeautifulSoup("<b>stop</b>")
tag = soup.new_tag("i")
tag.string = "Don't"
soup.b.string.insert_before(tag)
soup.b
# <b><i>Don't</i>stop</b>
```
`insert_after()` 方法在當前tag或文本節點后插入內容:
```
soup.b.i.insert_after(soup.new_string(" ever "))
soup.b
# <b><i>Don't</i> ever stop</b>
soup.b.contents
# [<i>Don't</i>, u' ever ', u'stop']
```
## clear()
`Tag.clear()` 方法移除當前tag的內容:
```
markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
soup = BeautifulSoup(markup)
tag = soup.a
tag.clear()
tag
# <a href="http://example.com/"></a>
```
## extract()
`PageElement.extract()` 方法將當前tag移除文檔樹,并作為方法結果返回:
```
markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
soup = BeautifulSoup(markup)
a_tag = soup.a
i_tag = soup.i.extract()
a_tag
# <a href="http://example.com/">I linked to</a>
i_tag
# <i>example.com</i>
print(i_tag.parent)
None
```
這個方法實際上產生了2個文檔樹: 一個是用來解析原始文檔的 `BeautifulSoup` 對象,另一個是被移除并且返回的tag.被移除并返回的tag可以繼續調用 `extract` 方法:
```
my_string = i_tag.string.extract()
my_string
# u'example.com'
print(my_string.parent)
# None
i_tag
# <i></i>
```
## decompose()
`Tag.decompose()` 方法將當前節點移除文檔樹并完全銷毀:
```
markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
soup = BeautifulSoup(markup)
a_tag = soup.a
soup.i.decompose()
a_tag
# <a href="http://example.com/">I linked to</a>
```
## replace_with()
`PageElement.replace_with()` 方法移除文檔樹中的某段內容,并用新tag或文本節點替代它:
```
markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
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)
a_tag
# <a href="http://example.com/">I linked to <b>example.net</b></a>
```
`replace_with()` 方法返回被替代的tag或文本節點,可以用來瀏覽或添加到文檔樹其它地方
## wrap()
`PageElement.wrap()` 方法可以對指定的tag元素進行包裝 \[8\] ,并返回包裝后的結果:
```
soup = BeautifulSoup("<p>I wish I was bold.</p>")
soup.p.string.wrap(soup.new_tag("b"))
# <b>I wish I was bold.</b>
soup.p.wrap(soup.new_tag("div"))
# <p><b>I wish I was bold.</b></p>
```
該方法在 Beautiful Soup 4.0.5 中添加
## unwrap()
`Tag.unwrap()` 方法與 `wrap()` 方法相反.將移除tag內的所有tag標簽,該方法常被用來進行標記的解包:
```
markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
soup = BeautifulSoup(markup)
a_tag = soup.a
a_tag.i.unwrap()
a_tag
# <a href="http://example.com/">I linked to example.com</a>
```
與 `replace_with()` 方法相同, `unwrap()` 方法返回被移除的tag