您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

带标签python

5b51 2022/1/14 8:22:54 python 字数 1142 阅读 571 来源 www.jb51.cc/python

我想要以下功能. input : this is test <b> bold text </b> normal text expected output: this is test normal text 即删除指定标签的内容 使用BeautifulSoup的解决方案: from BeautifulSoup import BeautifulSoup def removeTag(soup, tagn

概述

input : this is test <b> bold text </b> normal text
expected output: this is test normal text

删除指定标签内容

from BeautifulSoup import BeautifulSoup
def removeTag(soup,tagname):
    for tag in soup.findAll(tagname):
        contents = tag.contents
        parent = tag.parent
        tag.extract()

s = BeautifulSoup("abcd <b> btag </b> hello <d>dtag</d>")

removeTag(s,"b")
print s
removeTag(s,"d")
print s

收益:

>>>
abcd  hello <d>dtag</d>
abcd  hello

总结

以上是编程之家为你收集整理的带标签python全部内容,希望文章能够帮你解决带标签python所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶