当前位置:天才代写 > tutorial > Python教程 > python去除html标签

python去除html标签

2017-11-02 08:00 星期四 所属: Python教程 浏览:509

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 = '<em productIndex="0" class="valor-dividido" style="display:block"><span>ou <strong><label productIndex="0" class="skuBestInstallmentNumber">12</label>X</strong> de <strong> <label productIndex="0" class="skuBestInstallmentValue">R$  116,58</label></strong> sem juros</span></em>'

print strip_tags(html)

 

    关键字:

天才代写-代写联系方式