当前位置:Gxlcms > Python > python去除html标签

python去除html标签

时间:2021-07-01 10:21:17 帮助过:80人阅读

python去除html标签,自己写的,若有不足请指正:

#! /usr/bin/env python
#coding=utf-8
# blueel 2013-01-19
from HTMLParser import HTMLParser
  
class MLStripper(HTMLParser):
    def __init__(self):
        self.reset()
        self.fed = []
    def handle_data(self, d):
        self.fed.append(d)
    def get_data(self):
        return ''.join(self.fed)
  
def strip_tags(html):
    s = MLStripper()
    s.feed(html)
    return s.get_data()

调用:

html = 'ou X de sem juros'


print strip_tags(html)

人气教程排行