class MyParser(HTMLParser): def __init__(self,key): self.data=[] self.key=key self.falg=False self.linkname=‘‘ HTMLParser.__init__(self) def handle_starttag(self,tag,attrs): if self.key and tag ==self.key: self.falg=True def handle_data(self,data): if self.falg and data: self.data.append(unicode(eval(repr(data)),"utf-8")) def handle_endtag(self,tag): if self.key and tag ==self.key: self.falg=False def getresult(self): return self.data def get_html_tagdata(data,tag): IParser = MyParser(tag) IParser.feed(data) result=IParser.getresult() IParser.close() return result
时间: 2024-10-09 18:44:06