关于BeautifulSoup的追加方法的困惑

时间:2013-01-05 14:53:01

标签: python beautifulsoup

我试着测试下面的内容。现在我看到了一个令人怀疑的事情如下:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup("<a>Foo</a>")
>>> soup.a.append("Bar")
>>> soup
<a>FooBar</a>
>>> soup.a.contents
[u'Foo', u'Bar']
>>>

我很困惑为什么它以[u'Foo', u'Bar']代替[u'FooBar']

你能帮助我这个概念吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

>>> from BeautiulSoup import NavigableString
>>> soup = BeautifulSoup("<a>Foo</a>")
>>> soup.a.contents = [NavigableString(str(soup.a.contents[0]) + 'Bar')]
>>> soup
<a>FooBar</a>