在Beautiful Soup中向p标签添加文本
我在想有没有人知道怎么往一个标签里添加文字,比如
、这些标签,任何你想放字符数据的标签。文档里没有提到你可以怎么做。
2 个回答
1
>>> import BeautifulSoup
>>> b=BeautifulSoup.BeautifulSoup("<p></p><p></p>")
>>> for t,s in zip(b,[u'hello',u'world']):
... t.contents.append(BeautifulSoup.NavigableString(s))
...
>>> b
<p>hello</p><p>world</p>
当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。
8
我不太确定这是不是你想要的,但也许可以作为一个开始...
from BeautifulSoup import BeautifulSoup, NavigableString
html = "<p></p>"
soup = BeautifulSoup(html)
ptag = soup.find('p')
ptag.insert(0, NavigableString("new"))
print ptag
输出结果
<p>new</p>
文档中还有一些类似的例子:http://www.crummy.com/software/BeautifulSoup/documentation.html#Modifying%20the%20Parse%20Tree