python中的继承
星期五, 06月 15th, 2007今天用python中的SGMLParser写了一个HTMLParser功能其实就是将非标签数据抽取出来
import sgmllib class HTMLParser(sgmllib.SGMLParser): def __init__(self): sgmllib.SGMLParser.__init__(self) self.data = '' def handle_data(self,data): self.data += data def parse(html): p = HTMLParser() p.feed(html) p.close() return p.data if __name__ == '__main__': f = open("test.html") html = f.read() print parse(html)
在父类SGMLParser中的handle_data(data)是空的。所以我们只要将相应的操作写在这里就OK了。有点像汇编中的一个中断调用中嵌入的某个中断(好像是INT 10H),你可以为这个中断写代码以完成相应的操作。
继承还是很有用滴^O^