带标签python

前端之家收集整理的这篇文章主要介绍了带标签python前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想要以下功能.
  1. input : this is test <b> bold text </b> normal text
  2. expected output: this is test normal text

删除指定标签内容

解决方法

使用BeautifulSoup的解决方案:
  1. from BeautifulSoup import BeautifulSoup
  2. def removeTag(soup,tagname):
  3. for tag in soup.findAll(tagname):
  4. contents = tag.contents
  5. parent = tag.parent
  6. tag.extract()
  7.  
  8. s = BeautifulSoup("abcd <b> btag </b> hello <d>dtag</d>")
  9.  
  10. removeTag(s,"b")
  11. print s
  12. removeTag(s,"d")
  13. print s

收益:

  1. >>>
  2. abcd hello <d>dtag</d>
  3. abcd hello

猜你在找的Python相关文章