使用del a['href']
代替,就像你在一个普通的词典:
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup('<p>Hello <a href="http://google.com">Google</a></p>')
for a in soup.findAll('a'):
del a['href']
为您提供:
>>> print str(soup)
<p>Hello <a>Google</a></p>
UPDATE:
如果你想摆脱的<a>
标签共有,您可以使用.replaceWithChildren()
方法:
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup('<p>Hello <a href="http://google.com">Google</a></p>')
for a in soup.findAll('a'):
del a['href']
为您提供:
:
>>> print str(soup)
<p>Hello Google</p>
...而且,你在评论请求(包装用空格标签的文本内容),可实现
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup('<p>Hello <a href="http://google.com">Google</a></p>')
for a in soup.findAll('a'):
del a['href']
a.setString(' %s ' % a.text)
为您提供:
>>> print str(soup)
<p>Hello <a> Google </a></p>
你可以留下无意义的''吗?你可以'删除'['href']'。 – Ryan